[ACM] zoj 3818 Pretty Poem (2014 ACMICPC Regional 牡丹江站网络赛 J题)

来源:互联网 发布:北大青鸟java 编辑:程序博客网 时间:2024/05/16 06:38

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 AB 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

3niconiconi~pettan,pettan,tsurupettanwafuwafu

Sample Output

YesYesNo

Author: JIANG, Kai
Source: The 2014 ACM-ICPC Asia Mudanjiang Regional First Round


解题思路:

题意为输入一个只包含字母和标点符号的字符串,问该字符串是否符合 "ABABA" or "ABABCAB"的形式,其中A,B,C为原字符串中连续的不相同的子串。

比如niconiconi 符合ABABA的形式,因为 A= ni  B= con, 判断的时候要忽略掉字符串中的标点符号

思路为首先提取出来字符串中的字母,然后分别判断是否符合以上两种形式,判断ABABA的时候枚举A的长度,那么B的长度也就确定了,判断ABABCAB的时候,枚举A,B的长度,那么C的长度也就确定了。做题中出现的问题是char s[60],输入字符串的时候用了cin>>s,一直WA,换了gets(s)以后就过了,难道测试数据中字符串包含空格?可是空格不是标点符号啊。。。

代码:

#include <iostream>#include <stdio.h>#include <algorithm>#include <stack>#include <queue>#include <iomanip>#include <cmath>#include <string.h>using namespace std;#define ll long longconst int inf=0x3f3f3f3f;int n;int main(){    cin>>n;    getchar();    while(n--)    {        char s[60];        char temp[60];        gets(s);        int l=strlen(s);        if(l<5)        {            cout<<"No"<<endl;            continue;        }        int len=0;        for(int i=0;i<l;i++)        {            if((s[i]>=65&&s[i]<=90)||(s[i]>=97&&s[i]<=122))            {                temp[len]=s[i];                len++;            }        }        temp[len]='\0';        if(len<5)        {            cout<<"No"<<endl;            continue;        }        //ABABA的情况        int la=0,lb=0;//A的长度,B的长度        bool ok;        for(la=1;;la++)        {            ok=0;            if(len-3*la<2)                break;            if((len-3*la)%2!=0)                continue;            lb=(len-3*la)/2;            string A="";            string B="";            for(int i=0;i<la;i++)                A+=temp[i];            for(int i=la;i<la+lb;i++)                B+=temp[i];            if(A==B)                continue;            string ans="";            ans=A+B+A+B+A;            int i;            for(i=0;i<len&&ans[i]==temp[i];i++){}            if(i==len)            {                ok=1;                break;            }        }        if(ok)        {            cout<<"Yes"<<endl;            continue;        }        //ABABCAB的情况        int lc=0;        for(la=1;la<len-3;la++)        {            ok=0;            for(lb=1;lb<len-3;lb++)            {                if(3*la+3*lb>=len)                    break;                if(len-3*la-3*lb<0)                    break;                lc=len-3*la-3*lb;                string A="";                string B="";                string C="";                for(int i=0;i<la;i++)                    A+=temp[i];                for(int i=la;i<la+lb;i++)                    B+=temp[i];                for(int i=(la+lb)*2;i<(la+lb)*2+lc;i++)                    C+=temp[i];                if(A==B||A==C||B==C)//注意这一点,ABC不能相同                    continue;                string ans="";                ans=A+B+A+B+C+A+B;                int i;                for(i=0;i<len&&ans[i]==temp[i];i++){}                if(i==len)                {                    ok=1;                    break;                }            }            if(ok)                break;        }        if(ok)            cout<<"Yes"<<endl;        else            cout<<"No"<<endl;    }    return 0;}



1 0
原创粉丝点击