您的位置:首页 > 理论基础 > 计算机网络

ZOJ 3818 The Himalayas (2014 ACM/ICPC 牡丹江站 网络预选赛 J 题)

2014-09-14 14:46 357 查看
Pretty Poem

Time Limit: 2 Seconds
Memory Limit: 65536 KB

Poetry is a form of literature that uses aesthetic and rhythmic qualities of language. There are many famous poets in the contemporary era. It is said that a few ACM-ICPC contestants can even write poetic code. Some poems has a strict rhyme scheme like "ABABA"
or "ABABCAB". For example, "niconiconi" is composed of a rhyme scheme "ABABA" with A = "ni" and B = "co".

More technically, we call a poem pretty if it can be decomposed into one of the following rhyme scheme: "ABABA" or "ABABCAB". The symbol
A, B and C are different continuous non-empty substrings of the poem. By the way, punctuation characters should be ignored when considering the rhyme scheme.

You are given a line of poem, please determine whether it is pretty or not.

Input

There are multiple test cases. The first line of input contains an integer
T indicating the number of test cases. For each test case:

There is a line of poem S (1 <= length(S) <= 50). S will only contains alphabet characters or punctuation characters.

Output

For each test case, output "Yes" if the poem is pretty, or "No" if not.

Sample Input

3
niconiconi~
pettan,pettan,tsurupettan
wafuwafu

Sample Output

Yes
Yes

No


题目大意: 给你多个字符串,分别判断每个字符串在去除非字母字符后是否满足可以被分为 ABABA 或者 ABABCAB 形式;

解题思路:纯粹的模拟题,暴力即可;

话虽如此,但还是WA了几遍,我是有多水






#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <sstream>
#include <vector>
#include <ostream>
#include <string>
#include <cstdlib>
#include <cmath>
#define PI 3.141592653
using namespace std;
typedef long long LL;

char str[50+10];
int p;

bool check( )
{
    char stra[55];
    char strb[55];
    char kstr[55];
    char aa[55];
    char bb[55];
    char cc[55];
    for(int i = 1;i < p; i++)// 判定其是否为 ABABA 形式的字符串
    {
        if((p - 3*i)%2==0&&(p-3*i)>0)
        {
            int ka=0;
            int kb=0;
            int j;
            for(j = 0;j < i; j++)
            {
                stra[ka++] = str[j];
            }
            stra[ka]='\0';
            for(;j < i+(p-3*i)/2; j++)
            {
                strb[kb++] = str[j];
            }
            strb[kb]='\0';
            if(strcmp(stra,strb)==0)
                continue;
            sprintf(kstr,"%s%s%s%s%s",stra,strb,stra,strb,stra);
            if(strcmp(kstr,str)==0)
                return 1;
            kstr[0]=='\0';
        }
    }
    for(int i = 1;i <= p; i++)// 判断其是否为 ABABCAB形式的字符串
    {
        if((p-3*i)>3)
        {
            int j,k;
            for(j = 1;j < p; j++)
            {
                int ka,kb,kc;
                ka=kb=kc=0;
                if((p-3*i-j)%3==0&&(p-3*i-j)>=3)
                {
                    for(k = 0; k < i; k++)
                    {
                        aa[ka++]=str[k];
                    }
                    aa[ka]='\0';
                    for(k = i;k < i+(p-3*i-j)/3; k++)
                    {
                        bb[kb++]=str[k];
                    }
                    bb[kb]='\0';
                    int pk=i*2+2*(p-3*i-j)/3;
                    for(k=pk ;k < pk+j; k++)
                    {
                        cc[kc++]=str[k];
                    }
                    cc[kc]='\0';
                   // printf("A->%d B->%d C->%d  \n",i,(p-3*i-j)/3,j);
                    if(strcmp(aa,bb)==0||strcmp(aa,cc)==0||strcmp(bb,cc)==0)
                    {
                        continue;
                    }
                   // printf("A->%s  B->%s  C->%s\n",aa,bb,cc);
                    sprintf(kstr,"%s%s%s%s%s%s%s",aa,bb,aa,bb,cc,aa,bb);
                    if(strcmp(kstr,str)==0)
                        return 1;
                    kstr[0]='\0';
                }
            }
        }
    }
    return 0;
}

int main( )
{
    int t;
    cin>>t;
    getchar( );//抵消换行符
    while(t--)
    {
        char c;
        p = 0;
        while((c=getchar())!='\n')
        {
            if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
            {
                str[p++] = c;
            }
        }
        str[p] = '\0';//这一步很重要
        if(check( ))
        {
            cout<<"Yes"<<endl;
        }
        else
        {
            cout<<"No"<<endl;
        }
    }
  //  while(1);
    return 0;
}


如有BUG,欢迎指出!!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: