CodeChef-IOPC17A-The Birthday Tears(模拟)

本文介绍了一种通过循环模拟实现的字符串处理算法,该算法能够处理多个字符串,通过不断从中点剪切来寻找最长的由相同字符组成的子串,并考虑了长度为奇数的情况。文章提供了一份AC代码作为实现示例。

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

传送门:https://vjudge.net/problem/CodeChef-IOPC17A


题意:给定t个字符串,通过多次从中间剪切得到最长的每个字符都相同的字串,长度为奇数的串不能剪切,这时输出-1。for循环模拟,当时做的时候没想起来。

AC代码:

#include<cstdio>
#include<iostream>
#include<string>
using namespace std;

int main()
{
    int t, len, tot, j, flag, step;
    string s;
    scanf("%d", &t);
    while(t--)
    {
        step = 0;
        flag = 1;
        tot = 1;//the number of string segmentation
        cin >> s;
        len = s.size();
        while(1)
        {
            for(int i = 0; i < tot; i++)
            {
                flag = 1;
                for(j = i * len / tot; j < (i + 1)*len / tot - 1; j++)
                {
                    if(s[j] != s[j + 1])
                    {
                        flag = 0;
                        break;
                    }
                }
                if(flag)
                {
                    break;
                }
            }
            if(flag || (len / tot) & 1)
            {
                break;
            }
            else
            {
                step++;
                tot <<= 1;
            }
        }
        if(flag)
        {
            printf("%d\n", step);
        }
        else
        {
            printf("-1\n");
        }

    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值