[ACM] zoj 3818 Pretty Poem (2014 ACMICPC Regional 牡丹江站網路賽 J題),zojacmicpc
Pretty PoemTime 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
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;}