Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2594
Analysis: To determine whether there are substrings in two strings, can be used as s1 prefix && s2 suffix. Consider merging the two strings and then directly using the next array. The condition that may occur after merging is that the substring is greater than S1 | | The length of the S2, so you have to make a judgment.
/*Simpsons ' Hidden talentstime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) Total Submissio N (s): 3522 Accepted Submission (s): 1313Problem Descriptionhomer:marge, I just figured out a-to discover some of th E talents we weren ' t aware we had. Marge:yeah, what's it? Homer:take me for example. I want to find out if I had 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 prefixin Clinton ' s name th At are a suffix in my name. That's how close I'm to being a politician like clintonmarge:why on Earth choose the longest prefix so is a suffix??? Homer:well, our talents is deeply hidden within ourselves, Marge.Marge:So how close is you? homer:0! Marge:i ' m not surprised. Homer:but know, you must has some real math talent hidden deep in. Marge:how come? Homer:riemann and Marjorie gives 3!!! Marge:who the heck is Riemann? Homer:never mind. Write a program this, when GIven strings S1 and S2, finds the longest prefix of S1 is a suffix of s2. Inputinput consists of the lines. The first line contains S1 and the second line contains S2. Assume all letters is in lowercase. Outputoutput consists of a single line that contains the longest string which is a prefix of S1 and a suffix of S2, followe D by the length of the. prefix. If the longest such string is the empty string, then the output should be 0.The lengths of S1 and S2 'll be in most 50000 . Sample Inputclintonhomerriemannmarjorie Sample Output0rie 3 sourcehdu 2010-05 Programming Contest*/#include<cstdio>#include<cstring>Const intMAXN =50000+Ten;intN, M, next[2*MAXN];CharS1[MAXN], S2[MAXN], p[2*MAXN];voidGetNext () {next[0] = -1; intj =0, k =-1; while(J < N+m) {//traversing the P-string after merging if(k = =-1|| P[k] = = P[j]) Next[++j] = + +K; ElseK =Next[k]; }}intMain () { while(~SCANF ("%s", S1)) {scanf ("%s", S2); N= strlen (S1); m =strlen (S2); memcpy (P, S1,sizeof(S1)); strcat (p, S2); GetNext (); if(!next[n+m]) printf ("0\n");//There is no direct output 0 Else{ intCNT = next[n+m]; S1[CNT]=' /'; if(n > M) {//compare the lengths of two strings if(CNT > m) printf ("%s%d\n", S2, m);//exceeds minimum string length direct output Elseprintf"%s%d\n", S1, CNT); } Else { if(CNT > N) printf ("%s%d\n", s1, N); Elseprintf"%s%d\n", S1, CNT); } } } return 0;}
Hdu 2594 Simpsons ' Hidden talents