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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

kuangbin专题十六 KMP扩展KMP HDU2594 Simpsons’ Hidden Talents

發布時間:2023/11/30 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 kuangbin专题十六 KMP扩展KMP HDU2594 Simpsons’ Hidden Talents 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Homer: Marge, I just figured out a way to discover some of the talents we weren’t aware we had.
Marge: Yeah, what is it?
Homer: Take me for example. I want to find out if I have a talent in politics, OK?
Marge: OK.
Homer: So I take some politician’s name, say Clinton, and try to find the length of the longest prefix
in Clinton’s name that is a suffix in my name. That’s how close I am to being a politician like Clinton
Marge: Why on earth choose the longest prefix that is a suffix???
Homer: Well, our talents are deeply hidden within ourselves, Marge.
Marge: So how close are you?
Homer: 0!
Marge: I’m not surprised.
Homer: But you know, you must have some real math talent hidden deep in you.
Marge: How come?
Homer: Riemann and Marjorie gives 3!!!
Marge: Who the heck is Riemann?
Homer: Never mind.
Write a program that, when given strings s1 and s2, finds the longest prefix of s1 that is a suffix of s2.
InputInput consists of two lines. The first line contains s1 and the second line contains s2. You may assume all letters are in lowercase.OutputOutput consists of a single line that contains the longest string that is a prefix of s1 and a suffix of s2, followed by the length of that prefix. If the longest such string is the empty string, then the output should be 0.
The lengths of s1 and s2 will be at most 50000.Sample Input clinton homer riemann marjorie Sample Output 0 rie 3


思路就是直接拼接,然后處理即可。剛開始什么都沒考慮,直接交了。后來一點點發現了問題。8Y

列舉幾個特殊樣例即可

ab
abab

abab
ab

abc
ab

ab
cabc

len1為s1的長度,len2為s2的長度,len為總長

可以發現如果Next[len]<==0 肯定為0

否則可能為0

就是循環節的長度大于s1 或者滿足了不大于s1但是最終長度大于s2

還有可能就是一個很短的循環節循環了好多次。


1 #include<stdio.h> 2 #include<string.h> 3 char s[100010],s1[50010]; 4 int len,lentemp1,lentemp2,Next[100010]; 5 6 void prekmp() { 7 int i,j; 8 j=Next[0]=-1; 9 i=0; 10 while(i<len) { 11 while(j!=-1&&s[i]!=s[j]) j=Next[j]; 12 Next[++i]=++j; 13 } 14 } 15 16 int main() { 17 //freopen("in","r",stdin); 18 while(~scanf("%s",s)) { 19 lentemp1=strlen(s); 20 scanf("%s",s1); 21 lentemp2=strlen(s1); 22 strcat(s,s1); 23 len=strlen(s); 24 prekmp(); 25 if(Next[len]>0) { 26 int i=Next[len]; 27 while(i>lentemp1||i>lentemp2) {//小于s1&&小于s2 找滿足條件的最大i 28 i=Next[i]; 29 } 30 if(i<=0) printf("0\n"); 31 else { 32 for(int j=0;j<i;j++) printf("%c",s[j]); 33 printf(" %d\n",i); 34 } 35 } else printf("0\n"); 36 } 37 }

?








轉載于:https://www.cnblogs.com/ACMerszl/p/10269106.html

總結

以上是生活随笔為你收集整理的kuangbin专题十六 KMP扩展KMP HDU2594 Simpsons’ Hidden Talents的全部內容,希望文章能夠幫你解決所遇到的問題。

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