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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Seek the Name, Seek the Fame POJ - 2752 (理解KMP函数的失配)既是S的前缀又是S的后缀的子串

發(fā)布時間:2023/12/4 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Seek the Name, Seek the Fame POJ - 2752 (理解KMP函数的失配)既是S的前缀又是S的后缀的子串 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題意:給一個字符串S, 求出所有前綴pre,使得這個前綴也正好是S的后綴。 輸出所有前綴的結束位置。

就是求前綴和后綴相同的那個子串的長度 ?然后從小到大輸出,主要利用next數(shù)組求解。

例如 “ababcababababcabab”, 以下這些前綴也同時是S的后綴

ab ?: ?位置2

abab ?: 位置4

ababcabab : 位置9

ababcababababcabab : 位置 18

分析與總結:

這題,關鍵在于對KMP的失配函數(shù)的理解。只要真正理解了,那么做出來完全不成問題。

next[i]的意義就是:前面長度為i的字串的【前綴和后綴的最大匹配長度】?

下面是后來在網(wǎng)上找的一個圖片,很形象. ?
?? ? ? ?

? ? ? ? ? ? ??

?

??????e.g.

i01

2

345678

9

1011121314151617

18

dpab

a

bcabab

a

babcabab

/0

next-10

0

120123

4

34345678

9

??????Out: 2 ?4 ?9 ?18
??????由于子串既是S的前綴又是后綴,所以除去字符串本身長度18外,次長的為在字符串結束符‘\0’處求得的next數(shù)組值9,

??????此時S的前綴為 ababcabab(012345678),S的后綴為 ababcabab(9-17)。接著找第三長的既是S的前綴又是S后綴的子串。

??????如果找next[17]處的值8,則不滿足是S后綴的要求,因為17本身的字符是被排除在外的,10-16亦是同理。

??????而對于9之前的子串有next[18]知既是S的前綴又是S的后綴。而next[9]的值4指明了位置在9前的子串的前后綴長度為4,

??????而該子串包含在S的前綴中,所以該子串的后綴也包含在S的前綴中,而S的前綴又是S的后綴,所以該子串的后綴也是S的后綴。

??????依次往前直到滿足條件的子串長度為0為止。

題目:

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

AC代碼

#include<stdio.h> #include<string.h> using namespace std; const int M=4e5+10; char dp[M]; int s[M],e[M]; void f(int x) {memset(s,0,sizeof(s));int i=0;int j=-1;s[0]=-1;while(i<x){if(j==-1||dp[i]==dp[j])s[++i]=++j;else j=s[j];} } int main() {while(~scanf("%s",dp)){memset(e,0,sizeof(e));int x=strlen(dp);f(x);int b=0,k,a=0,i,flag=0;i=x;while(i!=-1){e[a++]=i;i=s[i];if(i==0)break;}for(i=a-1; i>=1; i--)printf("%d ",e[i]);printf("%d\n",e[0]);}return 0; }

?

總結

以上是生活随笔為你收集整理的Seek the Name, Seek the Fame POJ - 2752 (理解KMP函数的失配)既是S的前缀又是S的后缀的子串的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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