https://www.luogu.com.cn/problem/P4824
在匹配过程中,j是在当前情况下已经有多少个字符匹配,但是
比如小字符串是abc,大字符串是ababbac,在第二个字符匹配完成后,j==2,在第三个字符中变为了a,是小字符串的第一个字母,所以在while循环中会把j变成1,第四个同理j变成2,在第五个字符中变成了b,既不是应该匹配字符c,所以重新开始开始,while会把j变成0,重新开始匹配
总之,while循环结束后,会j变成当前应该匹配到的字符串
const int N = 1000010;
int ne[N];
int st[N];
int vis[N];
void solve()
{
string str=" ", s=" ";
string sstr, ss;
cin >> sstr >> ss;
str += sstr;
s += ss;
for (int i = 2, j = 0; i <= ss.length(); i++)
{
while (j && s[i] != s[j + 1]) j = ne[j];
if (s[i] == s[j + 1]) j++;
ne[i] = j;
}
int top = 0, h = 0;
for (int i = 1, j = 0; i <= sstr.length(); i++)
{
while (j && str[i] != s[j + 1]) j = ne[j];
if (str[i] == s[j + 1]) j++;
st[i] = j;
vis[++top] = i;
if (j == ss.length())
{
top -= ss.length();
j = st[vis[top]];
}
}
for (int i = 1; i <= top; i++)
{
cout << str[vis[i]];
}
}