hdu 5455 Fang Fang(水题)

来源:互联网 发布:数据科学 r语言实战 编辑:程序博客网 时间:2024/05/22 04:55

题目链接:hdu 5455 Fang Fang

解题思路

处理出每个c的位置,如果有相邻两个c的距离小于2即为-1

代码

#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;const int maxn = 1e6 + 5;char s[maxn];int solve () {    int n = strlen(s);    vector<int> pos;    for (int i = 0; i < n; i++) {        if (s[i] == 'c')            pos.push_back(i);        else if (s[i] == 'f')            continue;        else            return -1;    }    if (pos.size() == 0)        return n % 2 + n / 2;    pos.push_back(pos[0] + n);    for (int i = 1; i < pos.size(); i++)        if (pos[i] - pos[i-1] <= 2) return -1;    return pos.size()-1;}int main () {    int cas;    scanf("%d%*c", &cas);    for (int kcas = 1; kcas <= cas; kcas++) {        gets(s);        printf("Case #%d: %d\n", kcas, solve());    }    return 0;}
0 0