J - 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.
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.
The lengths of s1 and s2 will be at most 50000.
clinton homer riemann marjorie
0 rie 3
应用next[len],注意一个最大长度min(lena,lenb)。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
const int maxlen = 1000100;
int n;
int m;
int arrnext[maxlen+1];
int res[maxlen + 1];
char mai[maxlen];
char sub[maxlen];
void getnext()
{
int i = 0;
int j = -1;
arrnext[0] = -1;
int len = strlen(sub);
while (i <= len)
{
if (j == -1 || sub[i] == sub[j])
{
j++;
i++;
arrnext[i] = j;
//if (i-arrnext[i] != i&&i%(i-arrnext[i]) == 0)
//{
// printf("%d %d\n", i, i / (i - arrnext[i]));
//}
}
else
j = arrnext[j];
}
//printf("%d\n",len%(len - arrnext[len])==0?len/(len - arrnext[len]):1);
}
void kmp()
{
getnext();
int ans = 0;
int i = 0;
int j = 0;
int lenmai = strlen(mai);
int lensub = strlen(sub);
while (i < lenmai)
{
while (i < lenmai&&j < lensub)
{
if (j == -1 || mai[i] == sub[j])
{
i++;
j++;
}
else
j = arrnext[j];
}
if (j == lensub)
{
j = 0;
ans++;
}
}//while
printf("%d\n", ans);
}
int t;
int main()
{
//scanf("%d", &t);
int key = 0;
while (scanf("%s", sub)!=EOF)
{
int lena = strlen(sub);
scanf("%s", sub + strlen(sub));
//printf("%s\n", mai);
//continue;
//int x;
//scanf("%d", &x);
//if (x == 0)
// break;
key++;
//scanf("%s",mai);
//scanf("%s", sub);
//if (sub[0] == '.')
//break;
//printf("Test case #%d\n", key);
//kmp();
getnext();
//printf("\n");
int lensub = strlen(sub);
int lenb = lensub - lena;
//int ans = lensub - arrnext[lensub];
//printf("%d\n", lensub%ans == 0 ? (lensub == ans ? ans : 0) : ans - (lensub%ans));
int ans = arrnext[lensub];
int ansmax = min(lena, lenb);
if (ans > ansmax)
ans = ansmax;
if (ans != 0)
{
for (int i = 0;i < ans;i++)
printf("%c", sub[i]);
printf(" ");
}
printf("%d\n", ans);
}
return 0;
}