単純に回したのでは時間切れとなったので
考え方を変更した。
P1ならIOI P2ならIOIOI
となるはずなので、IOIの連続出現回数をカウントしていく。
P1なら1回、P2なら2回連続で出現したらresをインクリメント
/*****************************
* 2012/07/15 *
* AOJ_Volume5_0538 *
* IOIOI *
* crane *
******************************/
#include<iostream>
#include<string>
using namespace std;
int main(){
int n;
while(cin >> n, n){
int m;
cin >> m;
string s;
cin >> s;
int count=0, res= 0;
for(int i=0; i<m;){
if(s.substr(i, 3) == "IOI"){
count++;
if(n <= count) res++;
}else {
count = 0;
}
if(count == 0) i++;
else i+=2;
}
cout << res << endl;
}
return 0;
}
Amazon
2012年7月15日日曜日
2012年7月10日火曜日
AOJ Volume1 0139: Snakes
A種は,">'"の後に"="が1個以上並んだ後、"#"が来て、さらに前と同じ個数の"="が来た後、"~"(半角チルダ)で終わる。
B種は,">^"の後に "Q="が1個以上並んだ後、"~~"で終わる。
B種は,">^"の後に "Q="が1個以上並んだ後、"~~"で終わる。
A種の例: >'====#====~ >'==#==~
B種の例: >^Q=Q=Q=Q=~~ >^Q=Q=~~
/************************
* 2012/07/10 *
* AOJ_Volume1_0139 *
* Snakes *
* crane *
*************************/
#include<iostream>
#include<string>
using namespace std;
int main(){
int n;
cin >> n;
while(n--){
string input;
cin >> input;
if(input.length() <5){
cout << "NA" << endl;
}else{
string s = input.substr(0,2); //どの蛇か
char e = input.at(input.length()-1); //A B尾判定
char e2 = input.at(input.length()-2);//B 尾判定
if(input.length() % 2 != 0){//lengthが偶数出ないものはAでもBでもない
cout << "NA" << endl;
}else if(s == ">'" && e == '~'){ //SnakeA
bool frag_A = true;
for(int i=2; i<input.length()/2; i++){
char c1 = input.at(i); //前から
char c2 = input.at(input.length()-i);//後ろから
if(c1 != '=' || c2 != '='){
cout << "NA" << endl;
frag_A = false;
break;
}
}
char a = input.at(input.length()/2 );
if(a == '#' && frag_A) cout << "A" << endl;
}else if(s == ">^" && e == '~' && e2 == '~'){ //SnakeB
bool frag_B = true;
for(int i=2; i<input.length()-2; i+=2){
string s1 = input.substr(i, 2);
if(s1 != "Q="){
cout << "NA" << endl;
frag_B = false;
break;
}
}
if(frag_B)cout << "B" << endl;
}else cout << "NA"<< endl;
}
}
return 0;
}
2012年7月4日水曜日
AOJ Volume1 0101:Aizu PR
サンプルを実行すると、データセット数が入力された後に入力が勝手に一つ入る現象が発生。対策としてcin.get()を入れた。
/****************************************************************************************
* 2012/04/29
* AIZU ONLINE JUDGE volume1_0101 *
* AIZU PR *
* HoshinoをHoshinaに置き換えて出力する *
* 英文は1000文字以下 *
* *
* Input *
* 最初にデータセット数nが与えられる。続いてn行の英文が与えられる。 *
* 各データセットは1行に英文が与えられる。英文は半角英数字・記号を含む。 *
* Output *
* 各データセットについて”Hoshino”を”Hoshina”に変換した英文(半角)を1行に出力する。*
*****************************************************************************************/
/***********************************************
****************Time limit:1sec*****************
****************Memory limit:32768KB************
************************************************/
#include<iostream>
#include<string>
int main(){
//入力1 データセット数nを受け取る
int n; //データセット数受け取り用変数
std::cin >> n; std::cin.get();
for(int i=0; i<n; i++){
std::string input; //データセット
std::getline(std::cin, input); //データ入力
int input_size = input.length();//データ長
for(int i=0; i<input_size; i++){
if(input.substr(i,7) == "Hoshino"){
input.replace(i,7,"Hoshina");
}
}
std::cout << input << std::endl;
}
return 0;
}
/****************************************************************************************
* 2012/04/29
* AIZU ONLINE JUDGE volume1_0101 *
* AIZU PR *
* HoshinoをHoshinaに置き換えて出力する *
* 英文は1000文字以下 *
* *
* Input *
* 最初にデータセット数nが与えられる。続いてn行の英文が与えられる。 *
* 各データセットは1行に英文が与えられる。英文は半角英数字・記号を含む。 *
* Output *
* 各データセットについて”Hoshino”を”Hoshina”に変換した英文(半角)を1行に出力する。*
*****************************************************************************************/
/***********************************************
****************Time limit:1sec*****************
****************Memory limit:32768KB************
************************************************/
#include<iostream>
#include<string>
int main(){
//入力1 データセット数nを受け取る
int n; //データセット数受け取り用変数
std::cin >> n; std::cin.get();
for(int i=0; i<n; i++){
std::string input; //データセット
std::getline(std::cin, input); //データ入力
int input_size = input.length();//データ長
for(int i=0; i<input_size; i++){
if(input.substr(i,7) == "Hoshino"){
input.replace(i,7,"Hoshina");
}
}
std::cout << input << std::endl;
}
return 0;
}
AOJ Volume11 1155: How can I satisfy thee? Let me count the ways...
問題文に
<formula> ::= 0 | 1 | 2 | P | Q | R |
-<formula> | (<formula>*<formula>) | (<formula>+<formula>)
とあったので一文字目にP Q Rはないと考えてcase文で一文字ずつ処理。
/****************************************************
* 2012/06/30 *
* AOJ Volume11 1155 *
* How can I satisfy thee? Let me count the ways... *
* crane *
*****************************************************/
/*------------------------------------------------------------------------*
<formula> ::= 0 | 1 | 2 | P | Q | R |
-<formula> | (<formula>*<formula>) | (<formula>+<formula>)
->一文字目にP Q Rはなし
*--------------------------------------------------------------------------*/
#include<iostream>
#include<string>
using namespace std;
#define MAX_L 80
string in;
int pos;
int Vp,Vq,Vr;
int formula();
int main(){
while(cin >> in){
if(in == ".") break;
int result = 0; //結果の初期化
for(int p=0; p<3; p++)
for(int q=0; q<3; q++)
for(int r=0; r<3; r++){
Vp=p; Vq=q; Vr=r;
pos = 0;
if(formula()==2) result++;
}
cout << result << endl;
}
return 0;
}
int formula(){
char c = in.at(pos); //一文字読む
pos++;
if(isdigit(c)) //数字なら数値を返す
return c-'0';
switch(c){
case 'P':
return Vp;
break;
case 'Q':
return Vq;
break;
case 'R':
return Vr;
break;
case '-':
return 2-formula();
break;
case '(':
int a = formula(); //()中の演算子左側
int tmp=0; //演算子用フラグ
if(in.at(pos) == '*')
tmp = 1;
pos++;
int b = formula(); //() 中の演算子右側
pos++; //)を飛ばすためpos++
/* * だった場合*/
if(tmp == 1){
return min(a, b);
}else{
/* + だった場合*/
return max(a, b);
}
break;
}
}
<formula> ::= 0 | 1 | 2 | P | Q | R |
-<formula> | (<formula>*<formula>) | (<formula>+<formula>)
とあったので一文字目にP Q Rはないと考えてcase文で一文字ずつ処理。
/****************************************************
* 2012/06/30 *
* AOJ Volume11 1155 *
* How can I satisfy thee? Let me count the ways... *
* crane *
*****************************************************/
/*------------------------------------------------------------------------*
<formula> ::= 0 | 1 | 2 | P | Q | R |
-<formula> | (<formula>*<formula>) | (<formula>+<formula>)
->一文字目にP Q Rはなし
*--------------------------------------------------------------------------*/
#include<iostream>
#include<string>
using namespace std;
#define MAX_L 80
string in;
int pos;
int Vp,Vq,Vr;
int formula();
int main(){
while(cin >> in){
if(in == ".") break;
int result = 0; //結果の初期化
for(int p=0; p<3; p++)
for(int q=0; q<3; q++)
for(int r=0; r<3; r++){
Vp=p; Vq=q; Vr=r;
pos = 0;
if(formula()==2) result++;
}
cout << result << endl;
}
return 0;
}
int formula(){
char c = in.at(pos); //一文字読む
pos++;
if(isdigit(c)) //数字なら数値を返す
return c-'0';
switch(c){
case 'P':
return Vp;
break;
case 'Q':
return Vq;
break;
case 'R':
return Vr;
break;
case '-':
return 2-formula();
break;
case '(':
int a = formula(); //()中の演算子左側
int tmp=0; //演算子用フラグ
if(in.at(pos) == '*')
tmp = 1;
pos++;
int b = formula(); //() 中の演算子右側
pos++; //)を飛ばすためpos++
/* * だった場合*/
if(tmp == 1){
return min(a, b);
}else{
/* + だった場合*/
return max(a, b);
}
break;
}
}
2012年7月3日火曜日
AOJ Volume0 0017: Caesar Cipher
"the " , "this", "that"のいずれかが見つかるまでシフトする。
キーは3つだったため、配列で回さずそのままifで通した。
/*************************
* 2012/07/03 *
* AOJ Volume0 0017 *
* Caesar Cipher *
* crane *
**************************/
#include<iostream>
#include<string>
using namespace std;
int main(){
string input;
while(getline(cin, input)){
for(int i=0; i<26; i++){
for(int j=0; j<input.length(); j++){
if('a'<=input.at(j) && input.at(j) <='z')
input.at(j) = ((input.at(j) - 'a' + 1) % 26) + 'a';
}
if(input.find("the")!=-1 || input.find("this")!=-1 || input.find("that")!=-1){
break;
}
}
cout << input << endl;
}
return 0;
}
キーは3つだったため、配列で回さずそのままifで通した。
/*************************
* 2012/07/03 *
* AOJ Volume0 0017 *
* Caesar Cipher *
* crane *
**************************/
#include<iostream>
#include<string>
using namespace std;
int main(){
string input;
while(getline(cin, input)){
for(int i=0; i<26; i++){
for(int j=0; j<input.length(); j++){
if('a'<=input.at(j) && input.at(j) <='z')
input.at(j) = ((input.at(j) - 'a' + 1) % 26) + 'a';
}
if(input.find("the")!=-1 || input.find("this")!=-1 || input.find("that")!=-1){
break;
}
}
cout << input << endl;
}
return 0;
}
ラベル:
AOJ,
AOJ_Volume0,
シーザー暗号,
暗号,
文字列
AOJ Volume0 0006: Reverse Sequence
文字列の入れ替えのみ。
/*************************
* 2012/03/21
* AOJ Volume0 0006
* Reverse Sequence
* crane
**************************/
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
int main(){
string input;
cin >> input;
reverse(input.begin(),input.end());
cout << input << endl;
return 0;
}
/*************************
* 2012/03/21
* AOJ Volume0 0006
* Reverse Sequence
* crane
**************************/
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
int main(){
string input;
cin >> input;
reverse(input.begin(),input.end());
cout << input << endl;
return 0;
}
登録:
投稿 (Atom)