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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【POJ - 2752】Seek the Name, Seek the Fame (KMP,公共前缀后缀长度及个数)

發布時間:2023/12/10 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【POJ - 2752】Seek the Name, Seek the Fame (KMP,公共前缀后缀长度及个数) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names to their newly-born babies. They seek the name, and at the same time seek the fame. In order to escape from such boring job, the innovative little cat works out an easy but fantastic algorithm:?

Step1. Connect the father's name and the mother's name, to a new string S.?
Step2. Find a proper prefix-suffix string of S (which is not only the prefix, but also the suffix of S).?

Example: Father='ala', Mother='la', we have S = 'ala'+'la' = 'alala'. Potential prefix-suffix strings of S are {'a', 'ala', 'alala'}. Given the string S, could you help the little cat to write a program to calculate the length of possible prefix-suffix strings of S? (He might thank you by giving your baby a name:)?

Input

The input contains a number of test cases. Each test case occupies a single line that contains the string S described above.?

Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000.?

Output

For each test case, output a single line with integer numbers in increasing order, denoting the possible length of the new baby's name.

Sample Input

ababcababababcabab aaaaa

Sample Output

2 4 9 18 1 2 3 4 5

題目大意:

給一個字符串S,找一個 S 的“前綴-后綴串”作為新生兒的名字(“前綴-后綴串”的定義是 既是S的前綴,又是S的后綴)?

比如:S = 'alala'。S的 “前綴-后綴串” 是{'a','ala','alala'}。給定字符串S,讓你計算S的可能? “前綴-后綴串”? 的長度。

解題報告:

? 求到最后一個字符的next數組,然后每次跳轉到next,,最后倒序輸出 就可以得到答案了

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int MAX = 2e6 + 5; char s[MAX]; int Next[MAX],len,ans[MAX]; void getnext() {Next[0] = -1;int k = -1,j = 0;int len = strlen(s);while(j < len) {if(k == -1 || s[k] == s[j]) {k++,j++;Next[j] = k;} else k = Next[k];} } int main() {int n;while(~scanf("%s",s)) {getnext();int j = strlen(s),tot = 0;while(j) ans[++tot] = j,j = Next[j];for(int i = tot; i>=1; i--) {printf("%d%c",ans[i], i == 1 ? '\n' : ' ');}}return 0; }

總結:

? 這也告訴我們next數組中只有next[0] 才= -1;其他的就算是沒有,也是=0;;;main函數中這個while(j)就可以證明這一點、、、

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的【POJ - 2752】Seek the Name, Seek the Fame (KMP,公共前缀后缀长度及个数)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。