题目
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5455
题目来源:2015沈阳网络赛第二题
简要题意:可以使用f,ff以及c后面跟大于1个f的串,问构成一个串起码要几个这样的串(可以环形移位)
数据范围: ∑|S|⩽106
题解
首先有非c,f的肯定是不行的。
没有c的话很容易推出结果为 ⌈|S|/2⌉ 。
有c的话对于一个c后面的f都只需要这个c就能带掉,而f的长度小于2就是非法的。
实现
我的方法是先特判没有cf的情况和没有c的情况。
读入之后记录所有c的位置得到一个数组,判断非法即相邻两个位置距离要大于等于 2 。
而环形的情况只要在后边多加一个就行了,即第一个往后移动
|S| 。
代码
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
const int N = 1E6+5;
char s[N];
vector<int> pos;
int solve(int len) {
int vlen = pos.size();
if (vlen == 0) return (len+1)/2;
pos.pb(len+pos[0]);
for (int i = 0; i < vlen; i++) {
if (pos[i] >= pos[i+1]-2) return -1;
}
return vlen;
}
int main()
{
int t, cas = 1;
scanf("%d", &t);
while (t--) {
pos.clear();
scanf("%s", s);
int len = strlen(s);
bool res = true;
for (int i = 0; i < len; i++) {
if (s[i] == 'c') {
pos.pb(i);
} else if (s[i] != 'f') {
res = false;
break;
}
}
printf("Case #%d: %d\n", cas++, res ? solve(len) : -1);
}
return 0;
}