数据结构(上)(自主模式) 祖玛(Zuma)

祖玛(Zuma)
Description
Let’s play the game Zuma!

There are a sequence of beads on a track at the right beginning. All the beads are colored but no three adjacent ones are allowed to be with a same color. You can then insert beads one by one into the sequence. Once three (or more) beads with a same color become adjacent due to an insertion, they will vanish immediately.

Note that it is possible for such a case to happen for more than once for a single insertion. You can’t insert the next bead until all the eliminations have been done.

Given both the initial sequence and the insertion series, you are now asked by the fans to provide a playback tool for replaying their games. In other words, the sequence of beads after all possible eliminations as a result of each insertion should be calculated.

Input
The first line gives the initial bead sequence. Namely, it is a string of capital letters from ‘A’ to ‘Z’, where different letters correspond to beads with different colors.

The second line just consists of a single interger n, i.e., the number of insertions.

The following n lines tell all the insertions in turn. Each contains an integer k and a capital letter Σ, giving the rank and the color of the next bead to be inserted respectively. Specifically, k ranges from 0 to m when there are currently m beads on the track.

Output
n lines of capital letters, i.e., the evolutionary history of the bead sequence.

Specially, “-” stands for an empty sequence.

Example

Input
ACCBA
5
1 B
0 A
2 B
4 C
0 A

Output

ABCCBA
AABCCBA
AABBCCBA
-
A

Restrictions
0 <= n <= 10^4

0 <= length of the initial sequence <= 10^4

Time: 2 sec

Memory: 256 MB

思路:直接双向链表模拟,有一个样例比较坑字符串长度为0,所以不能使用scanf读入字符串

#include
#include
#include
using namespace std;
char s[10005];
int zhu;
struct node{
	char ch;
	node* pre;
	node* next;
};
node *head;
node *tail;
node *li;
void init(){
	head = new node;
	tail = new node;
	head->ch = '#';	
	head->next = tail;
	head->pre = NULL;
	tail->ch = '#';
	tail->next = NULL;
	tail->pre = head;
	for(int i=0;s[i]!='\0';i++){
		li = new node;
		li->ch = s[i];
		if(head->next==tail){
			head->next = li;
			li->next = tail;
			li->pre = head;
			tail->pre = li;			
		}
		else{
			li->next=tail;
			li->pre = tail->pre;
			tail->pre->next = li;
			tail->pre = li;
		}
	}
}
node *charu(int pos,char ch){
	node *p=head;
	int cnt=1;
	p=p->next;
	while(cnt<=pos&&p!=tail){
		cnt++;
		p=p->next;
	}
	node *k = new node;
	k->ch=ch;
	k->next=p;
	k->pre=p->pre;
	p->pre->next=k;
	p->pre=k;
	zhu++;
	return p->pre;
}
void bianli(){
	node *p=head;
	p=p->next;
	while(p!=tail){
		printf("%c",p->ch);
		p=p->next;
	}
	printf("\n");
}
void del(node *x){
	if(!zhu)
	 return ;
	node *f = new node;
	node *b = new node;
	if(x!=head)
		f=x->pre;
	if(x!=tail)
		b=x->next;
	char ch=x->ch;
	int cnt=1;	
	while(f->ch==ch){
		cnt++;
		f=f->pre;
	}
	while(b->ch==ch){
		cnt++;
		b=b->next; 
	}
//	cout<
	if(cnt>=3){
		zhu-=cnt;
		f->next=b;
		b->pre=f;
		del(b);
	} 
	else return ;
}
int main(){
	gets(s); 
	init();
	zhu=strlen(s);	
	int n;
	int cc;
	int t;
	scanf("%d",&t);
	while(t--){
		scanf("%d %c",&n,&cc);
		del(charu(n,cc));
		if(zhu)bianli();
		else printf("-\n");
	}
	return 0;
}

你可能感兴趣的:(数据结构(上)(自主模式) 祖玛(Zuma))