本文主要是介绍hdu 5455 Fang Fang(水题),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目链接: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;elsereturn -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;
}
这篇关于hdu 5455 Fang Fang(水题)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!