hdu 5455 Fang Fang
2015-09-19 18:53
477 查看
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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 Fto split S according to aforementioned rules. Repetitive strings should be counted repeatedly.
[align=left]Sample Input[/align]
8
ffcfffcffcff
cffcfff
cffcff
cffcf
ffffcffcfff
cffcfffcffffcfffff
cff
cffc
[align=left]Sample Output[/align]
Case #1:3
Case #2: 2
Case #3: 2
Case #4: -1
Case #5: 2
Case #6: 4
Case #7: 1
Case #8: -1
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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 Fto split S according to aforementioned rules. Repetitive strings should be counted repeatedly.
[align=left]Sample Input[/align]
8
ffcfffcffcff
cffcfff
cffcff
cffcf
ffffcffcfff
cffcfffcffffcfffff
cff
cffc
[align=left]Sample Output[/align]
Case #1:3
Case #2: 2
Case #3: 2
Case #4: -1
Case #5: 2
Case #6: 4
Case #7: 1
Case #8: -1
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; char s[100006]; int main() { int t,i,ans,j,flag,k=1,a; scanf("%d",&t); getchar(); while (t--) { flag=0; gets(s); if (s[0]==' ') { printf("Case #%d: ",k); k++; printf("0\n"); continue; } for (i=0;s[i];i++){ if (s[i]!='c'&&s[i]!='f') {flag=1;break;} if (s[i]=='c') break; } if (s[i]=='\0') { printf("Case #%d: ",k); k++; printf("%d\n",i/2+i%2); continue; } a=i; ans=1; j=0; for (i=i+1;s[i];i++) { if (s[i]!='c'&&s[i]!='f') {flag=1;break;} if (s[i]!='c') j++; else { if (j>=2) { j=0; ans++; } else { flag=1; break; } } } if (j+a<2) flag=1; printf("Case #%d: ",k); if (flag) printf("-1\n"); else printf("%d\n",ans); k++; } }
相关文章推荐
- 阅读《代码大全》后的读书笔记(第一部分)
- 11991 - Easy Problem from Rujia Liu?(抽象数据结构)
- Parcelable和Serializable的区别
- java学习笔记图形程序设计及GUI应用小结(3)
- android-自定义TextView
- MySql简单sql使用
- Java基础——to string 的作用和用法
- ButterKnife用法
- 如何实现MFC窗口的最大化以及控件随最大化发生位置变动原因
- Unix高级编程学习笔记(3)共享内存
- Linux主机安全防护系列(三)ddos攻击防御
- ThinkPHP的配置
- 使用TaskManager爬取2万条代理IP实现自动投票功能
- 详解const int* p;int const *p;int *const p;const int *
- 简单选择排序
- iOS开发:开发者账号申请后真机调试详细步骤
- linux安装mysql步骤(unrecognized service错误)
- 【归纳备忘】收集了一些神奇的BT种子磁力链接搜索网址
- 痴人说梦——重读《围城》
- 一键安装mysql5.6