#include<bits/stdc++.h>
using namespace std;
vector<int> arrayvalue(string ch)
{
int i, index, l = ch.size();
vector<int>lps(l);
index = 0;
for(i=1;i<l; )
{
if(ch[i] == ch[index])
{
lps[i] = index +1;
index++;
i++;
}
else
{
if(index != 0)
index = lps[index - 1];
else
{
lps[i] = index;
i++;
}
}
}
return lps;
}
int kmp(string ch, string an)
{
int i, j, l = an.size();
vector<int>lps = arrayvalue(an);
i = j = 0;
int b = 0, r = 0;
while(i<ch.size())
{
if(ch[i] == an[j])
{
i++;
j++;
b = j;
}
else
{
if(j != 0)
j = lps[j-1];
else
i++;
}
if(r < b)
r = i - j;
}
return r;
}
int main()
{
int n;
string a, b;
cin >> n >> a >> b;
b += b;
int c = kmp(b, a);
cout << c << endl;
return 0;
}
using namespace std;
vector<int> arrayvalue(string ch)
{
int i, index, l = ch.size();
vector<int>lps(l);
index = 0;
for(i=1;i<l; )
{
if(ch[i] == ch[index])
{
lps[i] = index +1;
index++;
i++;
}
else
{
if(index != 0)
index = lps[index - 1];
else
{
lps[i] = index;
i++;
}
}
}
return lps;
}
int kmp(string ch, string an)
{
int i, j, l = an.size();
vector<int>lps = arrayvalue(an);
i = j = 0;
int b = 0, r = 0;
while(i<ch.size())
{
if(ch[i] == an[j])
{
i++;
j++;
b = j;
}
else
{
if(j != 0)
j = lps[j-1];
else
i++;
}
if(r < b)
r = i - j;
}
return r;
}
int main()
{
int n;
string a, b;
cin >> n >> a >> b;
b += b;
int c = kmp(b, a);
cout << c << endl;
return 0;
}
Comments
Post a Comment