#include <bits/stdc++.h>
#define LL long long
#define fread() freopen("in.in","r",stdin)
#define fwrite() freopen("out.out","w",stdout)
using namespace std;
#define maxn 5000040
string st;
string str;
string temp;
int nxt[maxn];
struct Node{
char x;
int j;
Node(char x_,int j_){
x = x_;
j = j_;
}
};
stack<Node>v;
void get_next()
{
memset(nxt,0,sizeof(nxt));
int i = 0 , j = -1 ;
nxt[0] = -1;
while( i < st.size() )
{
if( j == -1 || st[i] == st[j] )nxt[++i] = ++j;
else j = nxt[j];
}
}
void get_pos()
{
int i = 0 , j = 0;
while( i < str.size() )
{
if( j == -1 || str[i] == st[j] ){
i++ , j++;
v.push({str[i-1],j});
if( j == st.size() )
{
int len = st.size();
while(len--) v.pop();
if(v.empty()) j = 0;
else j = v.top().j;
}
}
else {
j = nxt[j];
}
}
int cnt = 0;
temp = "";
while(!v.empty()){
temp += v.top().x;
v.pop();
}
for(int i = temp.size()- 1; i >=0 ; --i)
printf("%c",temp[i]);
cout << endl;
}
int main()
{
while( cin >> st >> str )
{
get_next();
get_pos();
}
}
SCU - 4438(KMP)
最新推荐文章于 2024-04-18 23:25:48 发布