日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Period II(FZU-1901)

發(fā)布時(shí)間:2025/3/17 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Period II(FZU-1901) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Problem Description

For each prefix with length P of a given string S,if
S[i]=S[i+P] for i in [0..SIZE(S)-p-1],

then the prefix is a “period” of S. We want to all the periodic prefixs.

Input

Input contains multiple cases.
The first line contains an integer T representing the number of cases. Then following T cases.

Each test case contains a string S (1 <= SIZE(S) <= 1000000),represents the title.S consists of lowercase ,uppercase letter.

Output

For each test case, first output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the number of periodic prefixs.Then output the lengths of the periodic prefixs in ascending order.

Sample Input

4
ooo
acmacmacmacmacma
fzufzufzuf
stostootssto

Sample Output

Case #1: 3
1 2 3
Case #2: 6
3 6 9 12 15 16
Case #3: 4
3 6 9 10
Case #4: 2
9 12

題意:t 組數(shù)據(jù),每組給出一個(gè)字符串,對于字符串的所有前綴,若存在循環(huán)節(jié),輸出這個(gè)字符串的符合條件的個(gè)數(shù)與這個(gè)字符串的長度

思路:思路與?Seek the Name, Seek the Fame(POJ-2752)類似,但輸出的不是循環(huán)節(jié)的長度,而是前綴子串的長度,此外需要統(tǒng)計(jì)符合條件的子串個(gè)數(shù),可利用隊(duì)列來統(tǒng)計(jì)輸出

Source Program

#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<utility> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #define PI acos(-1.0) #define E 1e-9 #define INF 0x3f3f3f3f #define N 1000001 #define LL long long const int MOD=20091226; const int dx[]= {-1,1,0,0}; const int dy[]= {0,0,-1,1}; using namespace std;int Next[N]; char p[N]; int n; void getNext(){Next[0]=-1;int len=strlen(p);int j=0;int k=-1;while(j<len){if(k==-1||p[j]==p[k]) {k++;j++;Next[j]=k;}else{k=Next[k];}} } int main(){int t;scanf("%d",&t);int Case=1;while(t--){scanf("%s",p);int len=strlen(p);getNext();int i=len;queue<int> Q;while(i){i=Next[i];Q.push(i);}printf("Case #%d: %d\n",Case++,Q.size());printf("%d",len-Q.front());Q.pop();while(!Q.empty()){printf(" %d",len-Q.front());Q.pop();}printf("\n");}return 0; }

?

總結(jié)

以上是生活随笔為你收集整理的Period II(FZU-1901)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。