zoj 3831 Pretty Poem

题意:给出一个长度不超过50的字符串,判断它是不是pretty串(忽略标点符号)。pretty串的定义如下:如果一个串可以写成ABABA或者ABABCAB的形式,且A、B、C两两不相等,那么这个串就是pretty串。


题解:

暴力,这题我是醉了,各种WA。。。。。。。

其实枚举是对的,但是好多地方枚举的 数字大小关系不正确,比如 m/2>=k+1等等

代码:

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
char tmp[55],s[55];

bool is_same(char *s,int l1,int r1,int l2,int r2)
{
    if(r1-l1!=r2-l2) return 0;
    for(int i=l1,j=l2;i<=r1;i++,j++)
    if(s[i]!=s[j]) return 0;

    return 1;
}

int main()
{
    int T,n;
    scanf("%d",&T);

    while(T--)
    {
        scanf("%s",tmp);
        n=0;
        for(int i=0;i<strlen(tmp);i++)
            if(isalpha(tmp[i]))
            s[++n]=tmp[i];

        int ok=0;
        for(int k=1;k<=n/2;k++) //ABABA
            if(n-k+1>k && is_same(s,1,k,n-k+1,n)) // 前缀A == 后缀A
            {
                int m=n-k; 
                //下面这里,加条件 m/2>=k+1才 AC~~~~~~~~~~~~~
                if((m&1)==0 && m/2>=k+1 && !is_same(s,1,k,k+1,m/2) && is_same(s,1,m/2,m/2+1,m)) //A!=B && AB==AB
                {ok=1;
              //  printf("k=%d\n",k);
                break;}
            }

        for(int k=2;k<=n/2;k++) //ABABCAB
        {
           // printf("k=%d\n",k);
            if(is_same(s,1,k,n-k+1,n) && n-k+1>k && n>k*3) //前AB==后AB
            for(int i=1;i<=k-1;i++)
            if(!is_same(s,1,i,i+1,k) && is_same(s,1,k,k+1,2*k)) //A!=B  && AB==AB
            if(2*k+1>i  && !is_same(s,1,i,2*k+1,n-k) && !is_same(s,i+1,k,2*k+1,n-k)) // A!=C && B!=C
            {ok=1;
         //   puts("cao");
            break;}
            }
        if(ok) puts("Yes");
        else puts("No");
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值