POJ 2406 Power Strings(kmp),poj2406

來源:互聯網
上載者:User

POJ 2406 Power Strings(kmp),poj2406

Language:DefaultPower Strings
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 33335   Accepted: 13852

Description

Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).

Input

Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.

Output

For each s you should print the largest n such that s = a^n for some string a.

Sample Input

abcdaaaaababab.

Sample Output

143

Hint

This problem has huge input, use scanf instead of cin to avoid time limit exceed.

Source

Waterloo local 2002.07.01


求一個串的最小周期


對於一個串,如果abcdabc 那麼next[len]=3,那麼len-next【len】就大於len/2,那麼len%(len-next[len])!=0;而對於一個周期串ababab next[len]=4,此時len-next[len]應該等於

最小串的長度,所以是不是有最小周期就可以用len%(len-next[len])是否為0來判斷,(個人理解,如有錯,請想告)



#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<stack>#include<vector>#define L(x) (x<<1)#define R(x) (x<<1|1)#define MID(x,y) ((x+y)>>1)#define eps 1e-8using namespace std;#define N 100005char a[N];int len,next[N];void getfail(char *a){ int i,j; len=strlen(a); i=0;j=-1; next[0]=-1; while(i<len) { if(j==-1||a[i]==a[j]){ i++; j++; next[i]=j;}elsej=next[j]; }}int main(){int i,j;while(scanf("%s",a)){if(a[0]=='.') break;getfail(a);int ans=len%(len-next[len]);if(ans==0)printf("%d\n",len/(len-next[len]));elseprintf("1\n");}return 0;}







北大POJ2406

怎麼和你說呢···問題太大了
首先報錯是re,個人分析是棧溢出。大到1000000的資料,你卻用遞迴做資料的複製,這是500000次的遞迴調用···正常1000以上就很難了
再說你的思路和代碼,你是純枚舉的做法,本來就浪費時間,還用string和cin這些很慢的東西,即使寫的對也會tle。代碼上本來能用迴圈做的字串累加,非要寫成遞迴。

建議:先學學演算法再做題。代碼往往是最簡單的最有效。c++的一些東西,例如stl,map在合適的時候可以用;string這種完全可以手動的東西應該拋棄,太慢;至於cin cout,如果你看hit的話就會發現作者說過不要用!因為它也太慢,而且格式控制上不如scanf精確。

這道題應該是kmp的做法,純暴力枚舉的話如果代碼夠好也可以過。標稱自己網上搜吧
 
一道acm題目,解釋……

這題我POJ的Power Strings很像
應該就是那一題吧。
可以用KMP來做的,上面的代碼我也看不懂。
學習一下KMP吧,很有用的。

#include<stdio.h>#include<string.h>constint maxint=1000005;char s[maxint];int fail[maxint],len;voidfind_next(){int j,i;
fail[0]=-1;for(j=1;j<len;j++){for(i=fail[j-1];i>=0&&s[i+1]!=s[j];i=fail[i]);if(s[i+1]==s[j])
fail[j]=i+1;else
fail[j]=-1;}}intmain(){int t,x,j;while(scanf("%s",s)!=EOF&&strcmp(s,".")!=0){
t=1;
len=strlen(s);find_next();for(j=fail[len-1];j>=0;j=fail[j]){if(len%(len-j-1)==0){
x=len/(len-j-1);if(x>t)
t=x;break;}}printf("%d\n",t);}return0;}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.