题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5455
先判断一发,然后记下c出现的位置(还要考虑转回到头部的情况),总之有c总会比单个f或者双f更少。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 7 using namespace std; 8 9 const int maxn = 100010; 10 const int INF = (1<<18); 11 12 char str[maxn]; 13 int pos[maxn]; 14 15 int main() { 16 // freopen("in", "r", stdin); 17 int T; 18 int kase = 1; 19 scanf("%d", &T); 20 while(T--) { 21 memset(pos, 0, sizeof(pos)); 22 scanf("%s", str); 23 int len = strlen(str); 24 int cnt = 0; //how c 25 int flag = 0; //not 26 for(int i = 0; str[i]; i++) { 27 if(str[i] != ‘c‘ && str[i] != ‘f‘) { 28 flag = 1; 29 break; 30 } 31 if(str[i] == ‘c‘) { 32 cnt++; 33 } 34 } 35 if(flag) { 36 printf("Case #%d: -1\n", kase++); 37 continue; 38 } 39 if(cnt == 0) { 40 printf("Case #%d: %d\n",kase++, len / 2 + len % 2); 41 continue; 42 } 43 int fir = INF; 44 int cur = 0; 45 for(int i = 0; str[i]; i++) { 46 if(str[i] == ‘c‘) { 47 if(fir == INF) { 48 fir = i; 49 } 50 pos[cur++] = i; 51 } 52 } 53 pos[cur++] = fir + len; //circle 54 for(int i = 1; i < cur; i++) { 55 if(pos[i] - pos[i-1] <= 2) { 56 flag = 1; 57 break; 58 } 59 } 60 if(flag) { 61 printf("Case #%d: -1\n", kase++); 62 } 63 else { 64 printf("Case #%d: %d\n", kase++, cnt); 65 } 66 } 67 }
时间: 2024-10-19 06:55:18