标签:
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=5455
题意是
Fang Fang says she wants to be remembered.
I promise her. We define the sequence F of strings.
F0 = ‘‘f",
F1 = ‘‘ff",
F2 = ‘‘cff",
Fn = Fn−1 + ‘‘f", for n > 2
Write down a serenade as a lowercase string S in a circle, in a loop that never ends.
Spell the serenade using the minimum number of strings in F, or nothing could be done but put her away in cold wilderness.
#include <cstdio> #include <algorithm> #include <cstring> using namespace std; char str[1000000 + 100]; int main() { int T; scanf("%d", &T); int kase = 0; while(T--) { scanf("%s", str); int len = strlen(str); bool qita = false; for(int i=0; i<len&&!qita; i++){ if(str[i]!=‘c‘&&str[i]!=‘f‘) qita=true; } if(qita) { printf("Case #%d: -1\n", ++kase); continue; } int geshu = 0; for(int i=0; i<len&&str[i]!=‘c‘; geshu++, i++); //由于是首尾相接, 我们求出开头f的个数 // printf("geshu = %d\n", geshu); bool flog = true; int res = 0; for(int i=len-1; i>=0; i--){ if(str[i]==‘c‘){ res++; if(geshu < 2) { //此时的geshu就是当前的c后面f的个数 flog = false; break; } geshu = -1; } geshu++; } if(res == 0){ //处理没有c的情况 geshu /= 2; //没有c的时候我们求出的f个数是实际个数的两倍 if(geshu%2==0) res = geshu/2; else res = geshu/2+1; } if(flog) { printf("Case #%d: %d\n", ++kase, res); }else{ printf("Case #%d: -1\n", ++kase); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/xingxing1024/p/5337507.html