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
Author: JIANG, Kai
Source: The 2014 ACM-ICPC Asia Mudanjiang Regional First Round
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
#define PB push_back
#define MP make_pair
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define DWN(i,h,l) for(int i=(h);i>=(l);--i)
#define CLR(vis,pos) memset(vis,pos,sizeof(vis))
#define PI acos(-1.0)
#define INF 0x7FFFFFFF
#define LINF 1000000000000000000LL
#define eps 1e-8
typedef long long ll;
const int maxn=55;
char s[maxn];
int main()
{
int cas_t;
cin>>cas_t;
while(cas_t--)
{
scanf("%s",s);
int len,alen,blen;
len=strlen(s);
int j=0;
REP(i,len)
if( (s[i]>='a'&&s[i]<='z') || (s[i]>='A'&&s[i]<='Z') )
s[j++]=s[i];
len=j;
bool flag=false;
char s1[maxn],s2[maxn],s3[maxn],a[maxn],b[maxn],c[maxn];
for(int i=0;i*2<len;i++)
{
//ABABA
alen=len-i*2;
blen=i-alen;
if(alen<i)
{
REP(j,alen) a[j]=s[j];a[alen]=0; //A
memcpy(b,&s[alen],blen),b[blen]=0; //B
memcpy(c,&s[2*i],alen),c[alen]=0; //A
memcpy(s1,s,i),s1[i]=0; //AB
memcpy(s2,&s[i],i),s2[i]=0; //AB
if(strcmp(s1,s2)==0 && strcmp(a,c)==0 && strcmp(a,b)!=0)
{
flag=true;
break;
}
}
//ABABCAB
if(i*3<len && !flag)
{
FOR(alen,1,i-1)
{
blen=i-alen;
REP(j,alen) a[j]=s[j];a[alen]=0; //A
memcpy(b,&s[alen],blen),b[blen]=0; //B
memcpy(s1,s,i),s1[i]=0; //AB
memcpy(s2,&s[i],i),s2[i]=0; //AB
memcpy(s3,&s[len-i],i),s3[i]=0;//AB
memcpy(c,&s[2*i],len-3*i),c[len-3*i]=0;
if(strcmp(s1,s2)==0 && strcmp(s1,s3)==0 && strcmp(a,b)!=0 && strcmp(a,c)!=0 && strcmp(b,c)!=0)
{
flag=true;
break;
}
}
}
}
if(flag) puts("Yes");
else puts("No");
}
return 0;
}