题目链接:http://acm.scu.edu.cn/soj/problem.action?id=4438
题意:给出一个串a和串b,串b中如果有a串则删除,删除后b剩下的串再连到一起,再找串a,反复进行,直到不能再删除为止。求最后的串b。
思路:把串a的哈希求出,用栈来维护串b。
#include<algorithm>
#include<string.h>
#include<stdio.h>
#define ULL unsigned long long
using namespace std;
const int maxx=5*1e6+5;
const ULL Hash=131;
char strA[maxx],strB[maxx];
ULL per[maxx],tem[maxx],hb[maxx];
void init()
{
per[0]=1;
for(int i=1; i<maxx; i++)
{
per[i]=per[i-1]*Hash;
}
return ;
}
int main()
{
init();
while(~scanf("%s%s",strA+1,strB+1))
{
ULL ha=0,r=0;
hb[1]=0;
int lenA=strlen(strA+1),lenb=strlen(strB+1);
for(int i=1;i<=lenA;i++)
{
ha=ha*Hash+strA[i]-'a'+1;
}
for(int i=1;i<=lenb;i++)
{
tem[++r]=strB[i];
hb[r]=hb[r-1]*Hash+strB[i]-'a'+1;
// printf("%llu %llu\n",hb[r]-hb[r-lenA]*per[lenA],ha);
if(r>=lenA&&hb[r]-hb[r-lenA]*per[lenA]==ha)
{
r-=lenA;
}
}
for(int i=1;i<=r;i++)
{
// printf("%llu ",hb[i]);
printf("%c",tem[i]);
}printf("\n");
}
}