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 too 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 poemPrettyIf it can be decomposed into one of the following rhyme scheme: "Ababa" or "ababcab". The symbolA,BAndCAre different continuous non-empty substrings of the poem. By the way, punctuation characters shoshould 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 integerTIndicating the number of test cases. For each test case:
There is a line of PoemS(1 <= length (S) <= 50 ).SWill 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
Solution:
Enter a string containing only letters and punctuation marks and ask if the string conforms to the "Ababa" or "ababcab" format, where A, B, C is a continuous and different substring in the original string.
For example, niconiconi conforms to the Ababa format. Because a = Ni B = con, the punctuation marks in the string must be ignored during judgment.
The idea is to first extract the letters in the string, and then determine whether the above two forms are met. When judging ABABA, the length of a is enumerated, then the length of B is determined, when determining ababcab, enumerate the length of A and B, then the length of C is determined. The problem encountered during the question is Char s [60]. The input string uses CIN> S, which is always wa. After changing gets (s), it will pass, does the string in the test data contain spaces? But spaces are not punctuation marks...
Code:
# 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 = 0x3f3f3f; 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 case int LA = 0, lB = 0; // length of A, length of 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;} // case of 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) // note that ABC cannot be the same as 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 ;}
[ACM] zoj 3818 pretty poem (question J of the 2014 acmicpc regional Mudanjiang Network Competition)