100分AC的祖玛代码

#include<stdio.h>
#include<string.h>
using namespace std;
#define maxn 10001
struct Listnode
{
	char data='\0';
	Listnode*pre;
	Listnode*succ;
	Listnode() {}
	Listnode(char e, Listnode*p = NULL, Listnode*s = NULL) :data(e), pre(p), succ(s) {}
};
class List
{
private:
	int _size;
	Listnode*header, *trailer;
protected:
	void init();
public:
	List() { init(); }
	Listnode*insertafter(Listnode*p, char const&e);
	void cycle_delete(Listnode*x,char e);
	void remove(Listnode*p);
	Listnode*pos(int position, char const&e);
	Listnode*hheader() { return header; }
	Listnode*ttrailer() { return trailer; }
	int size() { return _size; }
};
void List::init()
{
	header = new Listnode;
	trailer = new Listnode;
	header->succ = trailer; header->pre = NULL;
	trailer->succ = NULL; trailer->pre = header;
	_size = 0;
}
Listnode*List::insertafter(Listnode*p, char const&e)
{
	_size++;
	Listnode*x = new Listnode(e, p, p->succ);
	p->succ = x; x->succ->pre = x;
	return x;
}
void List::cycle_delete(Listnode*x,char e)
{
	Listnode*pre1 = x->pre;
	Listnode*succ1 = x->succ;
	int count = 1;
	while (pre1->data == e)
	{
		pre1 = pre1->pre;
		count++;
	}
	while (succ1->data == e)
	{
		succ1 = succ1->succ;
		count++;
	}
	if (count >= 3)
	{
		x=pre1;
		while (pre1->succ->data == e)remove(pre1->succ);
		if(x->data!='\0'&&x->data==succ1->data)cycle_delete(x,x->data);
	}
}
void List::remove(Listnode*p)
{
	p->pre->succ = p->succ;
	p->succ->pre = p->pre;
	delete p;
	_size--;
}
Listnode*List::pos(int position, char const&e)
{
	Listnode*p = header;
	while (position--)p++;
	return insertafter(p, e);
}
int main()
{
	int n, length, i = 0,pos_i = 0;;//n是插入次数
	char p[maxn],c,e;
	Listnode*head;
	#ifndef _OJ_
	       freopen("input.txt", "r", stdin);
	       freopen("output.txt", "w", stdout);
	#endif
	if (c <= 'Z'&&'A' <= (c = getchar()))
	{
		p[0] = c;
		scanf("%s\n%d", &p[1], &n);
		//这里有个奇怪的现象,就是每次重新输入,p数组都是以全新的形式接收
	}
	else scanf("%d", &n);
	       // gets(p);可以用gets()函数获取一行字符串,但是这样的话,读取速度会变慢
	       // scanf("%d", &n);
	length = strlen(p);
	List zuma;
	Listnode*pos_s = zuma.hheader();
	while (length--) {
		pos_s = zuma.insertafter(pos_s, p[i++]);
	}
	while (n--) {
		
		scanf("%d %c", &pos_i,&e);
		if(pos_i<=zuma.size()/2)
		{
			head = zuma.hheader();
			while (pos_i--)head = head->succ;
		}
		else
		{
		head = zuma.ttrailer();
		while (pos_i++<=zuma.size())head = head->pre;
		}
		head = zuma.insertafter(head, e);
		zuma.cycle_delete(head,head->data);
		Listnode*headd = zuma.hheader()->succ;
		auto size_s = zuma.size();
		if (size_s > 0)while (size_s--) { printf("%c", headd->data); headd = headd->succ; }
		else printf("-");
		printf("\n");
	}
	return 0;}

猜你喜欢

转载自blog.csdn.net/hgtjcxy/article/details/80394537