BNU 26580 Software Bugs 将主串中的字串全部去掉 需要经常啃下的题

Software Bugs


Time Limit: 4000 ms     Case Time Limit: 4000 ms     Memory Limit: 65536 KB
Submit: 37     Accepted: 12

[Prev][Next]

Description


 

Not all bugs are insects. There are also certain kinds of bugs that may become a real nightmare for software developers. You definitely know the situation when a user calls to say “Hey, I’ve found a bug in your program.” Once you have located and removed the bug, another one appears immediately. It is a hard and never-ending process.

Recently, there appeared a promising open-source initiative called the Bug Preprocessor. The preprocessor is a program able to find all bugs in your source code and mark them, so they are relatively easy to remove. Your task is to write a program that will remove all marked bugs from the preprocessed source code.


Input


 

The input contains several test cases. Each test case starts with a line containing one integer numberT (0 ≤ T ≤ 100 000), one space and a stringB used by the preprocessor to mark all bugs. The next T lines then contain the preprocessed source code. All bugs are represented by a case-sensitive stringB.

Each line of the input will be between 0 and 2 000 000 characters long. The bug markerB consists of at least 1 and at most 1000 uppercase letters (“A” through “Z”).


Output


 

Your program must remove all of the bugs and produce the same result as if the first occurrence ofB on each line was removed repeatedly, until there is no other existing occurrence ofB. Nothing else may be removed, not even spaces.


Sample Input

7 BUG
print "No bugs here..."

void hello() {
BUGBUG
  printfBUG("Hello, world!\n");
}

1 ERR
wriERRERRtelERRn("Hello E-R-R");

Sample Output

print "No bugs here..."

void hello() {

  printf("Hello, world!\n");
}

writeln("Hello E-R-R");

Source

CTU Open Contest 2012

题意:
输入 子串 以及n  在下面n个主串中 将出现的字串全部去掉 输出

/*
思路:  看题目中注释

stage[x][y] 表示出现y的时候应该指向哪里

*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char b[200005];
char out[200005];
int state[200005];
char a[2000];
int stage[1000][26];//1000层 bug最多1000个大写字符  
int a_len,len;
int b_len=0;


void automat ()//假设bug 为 BUGBUG
{

	int back=0,i,j;
	memset(stage,0,sizeof(stage));/////////
	stage[0][a[0]-'A']=1;
	a_len=strlen(a);
	/*对于每层 对应的a[i]的位置 都往下一层走 其它的仍旧停在原来位置  这样在遍历b串的时候遇到与a串一样的字串
	就会一直走到a_len 这时候不保存这个子串 不输出  */
	for(i=1;i<a_len;i++)
	{
		stage[i][a[i]-'A']=i+1;//后面用back接受此值 表示第二次出现这个字符的时候应该将back指向下一层
		for(j='A';j<='Z';j++)
		{
			if(a[i]!=j)
			{
				stage[i][j-'A']=stage[back][j-'A'];//匹配不正确 赋值为上一次循环的数值
			}
		}
		back=stage[back][a[i]-'A'];
		/*对于BUGBUG 因为aut[0][B-'A']=1  如果这里a[i]再次等于'B'那么back就会变成1,否则依旧是0*/
	}
}
void add(char ch)
{
	int pos;
	pos=state[len];
	if(ch<'A'||ch>'Z')
	{
		out[len++]=ch;
		state[len]=0;//回到0层继续查找
		return ;
	}
    pos=stage[pos][ch-'A'];
	if(pos==a_len)//  pos==a_len  说明找到了 该串 不能输出  
	{
         len=len-(a_len-1);
		 return;
	}
	out[len++]=ch;
	state[len]=pos;
	

}
int main (void)
{
	int i,n,j;
	char *p;
	while(scanf("%d",&n)!=EOF)
    {
		getchar();
	    gets(a);
		automat();
		for (i=0;i<n;i++)
		{
			gets(b);
			b_len=strlen(b);
			len=0;
			state[0]=0;
			for(j=0;j<b_len;j++)
				add(b[j]);
			out[len]=0;
			puts(out);
		}
    }
	return 0;
}





你可能感兴趣的:(BNU 26580 Software Bugs 将主串中的字串全部去掉 需要经常啃下的题)