bugs.cpp
#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
using namespace std;
class node
{
public:
node * prev;
node * next;
char val;
node(char a)
{
prev = next = NULL;
val = a;
}
};
class linkedlist
{
public:
node * start;
node * end;
node * actual;
void create(const char * arr)
{
start = end = actual = NULL;
node * tmp = NULL;
node * old = NULL;
while(*arr)
{
tmp = new node(*arr);
tmp->prev = old;
if (old)
old->next = tmp;
else
start = tmp;
old = tmp; //printf("%s\n", megarr);
arr++;
}
if (tmp)
tmp->next = NULL;
end = tmp;
actual = start;
}
void goback(int steps)
{
while(steps--)
{
if (!actual->prev)
return;
actual = actual->prev;
}
}
bool contain(const char * str, int stringlen)
{
node * ptr = actual;
while(stringlen--)
{
if (!ptr || ptr->val != *(str++))
return false;
ptr = ptr->next;
}
return true;
}
void del(int size)
{
//print();
node * tosetprev = actual->prev;
while(size--)
{
node * tmp = actual;
actual = actual->next;
delete tmp;
}
if (!actual)
{
actual=tosetprev;
tosetprev->next = NULL;
}
else
{
actual->prev = tosetprev;
tosetprev->next = actual;
}
//print();
}
bool notEnd()
{
return actual;
}
void gonext()
{
actual = actual->next;
}
void print()
{
node * tmp = start;
while(tmp)
{
printf("%c",tmp->val);
tmp = tmp->next;
}
printf("\n");
}
};
int main ()
{
char * bug = new char[2000001];
int lines, buglen;
char * megarr = new char[2000001];
while ( scanf("%d %s\n", &lines ,bug) == 2 )
{
//cout << "line: " << lines << endl;
buglen = strlen(bug);
while (lines--)
{
cin.getline(megarr,2000000,'\n');
linkedlist ll;
ll.create(megarr);
while(ll.notEnd())
{
if (ll.contain(bug,buglen))
{
ll.del(buglen);
ll.goback(buglen);
}
else
{
ll.gonext();
}
}
ll.print();
}
}
return 0;
}