ZOJ,zoj題目分類
Description
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 symbolA, 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題意:求滿足"ABABA" or "ABABCAB"組合的字串思路:暴力求A和B,推出C#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>using namespace std;int main() {int t;char ch[60];scanf("%d", &t);while (t--) {string str;scanf("%s", ch);int len = strlen(ch);for (int i = 0; i < len; i++)if (isalpha(ch[i]))str += ch[i];len = str.length();int flag = 0;for (int i = 1; i < len/2 && !flag; i++) for (int j = 1; j < len/2 && !flag; j++) {string A = str.substr(0, i);string B = str.substr(i, j);if (A == B)continue;if (A + B + A + B + A == str) {flag = 1;continue;}if (len - (i + j) * 3 > 0) {string AB = A + B;string C = str.substr(2*(i+j), len-3*(i+j));if (A == C || B == C)continue;if (AB + AB + C + AB == str) {flag = 1;continue;}}}if (flag)printf("Yes\n");else printf("No\n");}return 0;}
zoj 1151
我做了好長時間才AC……
其實tle不是你的程式有問題,而是確實是你程式在啟動並執行過程中逾時了。就是說你程式啟動並執行很慢,結果最後超過1秒了就判定逾時。
逾時有很多原因,有的是演算法的問題,但是你的程式沒演算法問題。所以只能是另外的一種可能,就是輸入輸出的問題。
你使用的c++標準輸入輸出cin cout 還有標準字串類string,這些都是c++的對象或類,在使用的時候都要建立對象,使用起來速度很慢。在對小規模的acm問題時不影響,但是對輸入量非常大的時候,使用cin cout明顯比scanf和printf要慢的多。
string也是比vector<char>慢,而vector<char>又比char[]慢。。。
舉個例子,我曾經做一個題目,用scanf和printf用的0ms,而用cin和cout(沒改任何地方)用了700多ms……
我是用你的程式模板改成了char[]並把輸入輸出改了gets,printf,scanf等函數後AC的。
希望做ACM還是養成良好的習慣,輸入輸出盡量用C的函數吧,當然c++的標準函數也不是一無是處,stl裡面的有些函數就很快。比如sort等。
zoj 1151
我做了好長時間才AC……
其實tle不是你的程式有問題,而是確實是你程式在啟動並執行過程中逾時了。就是說你程式啟動並執行很慢,結果最後超過1秒了就判定逾時。
逾時有很多原因,有的是演算法的問題,但是你的程式沒演算法問題。所以只能是另外的一種可能,就是輸入輸出的問題。
你使用的c++標準輸入輸出cin cout 還有標準字串類string,這些都是c++的對象或類,在使用的時候都要建立對象,使用起來速度很慢。在對小規模的acm問題時不影響,但是對輸入量非常大的時候,使用cin cout明顯比scanf和printf要慢的多。
string也是比vector<char>慢,而vector<char>又比char[]慢。。。
舉個例子,我曾經做一個題目,用scanf和printf用的0ms,而用cin和cout(沒改任何地方)用了700多ms……
我是用你的程式模板改成了char[]並把輸入輸出改了gets,printf,scanf等函數後AC的。
希望做ACM還是養成良好的習慣,輸入輸出盡量用C的函數吧,當然c++的標準函數也不是一無是處,stl裡面的有些函數就很快。比如sort等。