HDU 5455(字符串)
2015-09-23 13:24
281 查看
Fang Fang
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 849 Accepted Submission(s): 350
Problem Description
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.
Input
An positive integer T,
indicating there are T test
cases.
Following are T lines,
each line contains an string S as
introduced above.
The total length of strings for all test cases would not be larger than 106.
Output
The output contains exactly T lines.
For each test case, if one can not spell the serenade by using the strings in F,
output −1.
Otherwise, output the minimum number of strings in F to
split Saccording
to aforementioned rules. Repetitive strings should be counted repeatedly.
Sample Input
8 ffcfffcffcff cffcfff cffcff cffcf ffffcffcfff cffcfffcffffcfffff cff cffc
Sample Output
Case #1: 3 Case #2: 2 Case #3: 2 Case #4: -1 Case #5: 2 Case #6: 4 Case #7: 1 Case #8: -1 Hint Shift the string in the first test case, we will get the string "cffffcfffcff" and it can be split into "cffff", "cfff" and "cff".
#include <iostream> #include <stdio.h> #include <string> #include <string.h> #include <cmath> #include <algorithm> #include <map> #include <set> #include <queue> using namespace std; const int inf=(1<<30); char s[2000000]; int main() { int t,cnt1=1; scanf("%d\n",&t); while(t--) { scanf("%s",s); int len=strlen(s); int t=len; int b=len; int cnt,mark; cnt=mark=0; for(int i=0;i<len;i++) { if(s[i]=='f') s[b++]='f'; if(s[i]=='c') break; } s[b]='\0'; int len1=strlen(s); printf("Case #%d: ",cnt1++); if(t*2==b) { if(len%2==0) printf("%d\n",len/2); else printf("%d\n",len/2+1); continue; } for(int i=0;i<len;i++) { if(s[i]=='c') cnt++; if(s[i]!='f'&&s[i]!='c') mark=1; if(s[i]=='c'&&s[i+1]!='f') mark=1; if(s[i]=='c'&&s[i+2]!='f') mark=1; if(s[len-1]=='c'&&s[0]!='f') mark=1; if(s[len-1]=='c'&&s[1]!='f') mark=1; } if(mark) printf("-1\n"); else printf("%d\n",cnt); } return 0; }
相关文章推荐
- 循环神经网络(RNN, Recurrent Neural Networks)介绍
- Techmark课程带给管理者的思考
- secureCRT连接ubuntu问题- The remote system refused the connection
- Unity导入FBX自动进行动画切分
- 获取ImageView中的图像
- Unity导入FBX自动进行动画切分
- Python中正则表达式的详细教程
- gcc/g++命令参数中文帮助手册
- quartz-2实例
- 直线提前量瞄准
- Android SurfaceView的总结与理解
- virtualbox安装centos6.5设置共享文件夹
- Eclipse下编写C++程序——CDT环境搭建
- Eclipse黑色主题设置方法
- Hustoj关闭Apache的目录浏览功能
- 推荐2一个在Java编码过程中得心应手的工具
- Notepad++ Emmet安装方法教程
- 将Sublime Text 添加到鼠标右键菜单的教程方法
- qml学习--------------图形的变换
- java输入的几种方式 转