社区讨论

TLE怎么改?

P12368[蓝桥杯 2022 省 Python B] 消除游戏参与者 2已保存回复 2

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
2 条
当前快照
1 份
快照标识符
@mhjrtqao
此快照首次捕获于
2025/11/04 07:28
4 个月前
此快照最后确认于
2025/11/04 07:28
4 个月前
查看原帖
上面是用链表做的,下面注释是用vector,都是85分三个点TLE
CPP
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
struct node {
    char s;
    node * pre,* next;
    int mark=0;
} ;
void addnode (node *last, node *n)
    {
        n->next=last->next;
        last->next=n;
        n->pre=last;
    }
int main(){
    node * head=NULL,*last=NULL;
    string inp;
    cin>>inp;
    int inpsize=inp.size();
    head=new node;
    head->next=NULL;
    head->pre=NULL;
    head->s=inp[0];
    last=head;
    for(int i=1;i<inpsize;i++){
        node *temp=new node;
        temp->s=inp[i];
        temp->next=NULL;
        temp->pre=NULL;
        addnode(last,temp);
        last=temp;
    }
    bool flag=true;
    for (int i=1;i<=10000000&&flag;i++)
        {
            flag=false;
            node *temp=head;
            while(temp!=NULL)
                {
                    if(temp->next!=NULL&&temp->pre!=NULL)
                    {
                        if(temp->s==temp->pre->s && temp->s!=temp->next->s)
                        {
                            temp->mark=1;
                            temp->next->mark=1;
                            flag=true;
                        }
                        if(temp->s==temp->next->s && temp->s!=temp->pre->s)
                        {
                            temp->mark=1;
                            temp->pre->mark=1;
                            flag=true;
                        }
                        
                    }
                    temp=temp->next;
                }
            temp=head;
            while(temp!=NULL)
                {
                    if (temp->mark==1)
                        {
                            if (temp==head)
                            {
                                if(temp->next!=NULL) temp->next->pre=NULL;
                                head=temp->next;
                            }
                            else 
                            {
                                temp->pre->next=temp->next;
                                if(temp->next!=NULL) temp->next->pre=temp->pre;
                            }
                        }
                    temp=temp->next;
                }
                if(head==NULL) 
				{
					flag=false;
				}
        }
        if(head==NULL) cout<<"EMPTY";
        else{
            node *temp=head;
            while (temp!=NULL)
                {
                    cout<<temp->s;
                    temp=temp->next;
                }
        }
}

/*int dele[1000006];
int change[1000006];
int main(){
	vector<char> myv;
	string inp;
	cin>>inp;
	int changnum;
	int length=inp.size();
	changnum=length;
	for(int i=0;i<length;i++)
	{
		change[i]=i;
		myv.push_back(inp[i]);
	}
	bool flag=true;
	for (int i=1;i<=1000000&&flag;i++)
	{
		int leng1=myv.size();
		flag=false;
		memset(dele,0, sizeof dele);
		for (int j=0;j<changnum;j++)
		{
			if(change[j]>=1&&change[j]<length-1&&myv[change[j]]==myv[change[j]-1]&&myv[change[j]]!=myv[change[j]+1]) 
			{
				dele[change[j]]=dele[change[j]+1]=1;
				flag=true;
			}	
			if(change[j]>=1&&change[j]<length-1&&myv[change[j]]==myv[change[j]+1]&&myv[change[j]]!=myv[change[j]-1]){
				dele[change[j]]=dele[change[j]-1]=1;
				flag=true;	
			} 
		}
		int cnt=0,cnt1=0;
		changnum=0;
		
		for(auto it=myv.begin();it!=myv.end();i)
		{
			
			if(dele[cnt]==1)
			{	
				cnt1++;
				it=myv.erase(it);
				if(cnt-cnt1>0){
					change[changnum]=cnt-cnt1-1;
					changnum++;
				}
				if(cnt-cnt1>=0){
				change[changnum]=cnt-cnt1;
				changnum++;
				}
			}
			else it++;
			cnt++;
			
		}
		if (myv.size()==0) flag=false;
	}
	int leng1=myv.size();
	if(leng1==0) cout<<"EMPTY";
	else for(int i=0;i<leng1;i++) cout<<myv[i];
	return 0;
}*/

回复

2 条回复,欢迎继续交流。

正在加载回复...