SCU4438-Censort

题目链接:http://acm.scu.edu.cn/soj/problem.action?id=4438


题意:给出两个字符串A, B,  要求每次将B中第一次出现的A删除, 然后将B剩下的两段前后合并, 然后重复这个过程直到没有可以删除的A位置, 问最后剩下的串是什么

解题思路:链表+kmp,对字符串A做kmp,然后和字符串B进行匹配


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f3f;

char a[5000010];
char b[5000010];
int nt[5000010];
int nex[5000010];
int p[5000010];
int len1, len2;

void getNext()
{
	nt[0] = -1;
	for (int i = 0; i < len1; i++)
	{
		int k=nt[i];
		while (k>0&&	a[i]!=a[k])
			k = nt[k];
		nt[i + 1] = k + 1;
	}
}

int main()
{
	while (~scanf("%s%s",a,b))
	{
		len1 = strlen(a), len2 = strlen(b);
		if (len1 > len2)
		{
			printf("%s\n", b);
			continue;
		}
		getNext();
		for (int i = 0; i <= len2; i++)
		{
			nex[i] = i + 1;
			p[i] = i - 1;
		}
		for (int i = 0, j = 0; i < len2;)
		{
			if (j < len1 && b[i] == a[j]) j++;
			else
			{
				while (j >=0&&b[i]!=a[j])
					j = nt[j];
				j++;
			}
			if (j == len1)
			{
				int x = i;
				for (int k = 0; k < len1; k++) i = p[i];
				if (i == -1)
				{
					p[nex[x]] = -1;
					i = nex[x];
					j = 0;
					continue;
				}
				else
				{
					nex[i] = nex[x];
					p[nex[x]] = i;
					for (int k = 1;k= 0; i--) printf("%c", a[i]);
		printf("\n");
	}
	return 0;
}

你可能感兴趣的:(----kmp,SCU)