hdu 5455 Fang Fang 2015沈阳网络赛 模拟

本文详细解析了HDU网络赛第二题的解题思路,通过判断字符串中字符种类并计算最少所需字符串数量,同时处理环形移位情况。主要关注字符串操作、环形逻辑与边界条件的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

题目链接: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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值