跳到正文

题解

题解:P12368 [蓝桥杯 2022 省 Python B] 消除游戏

yueluoxingchen约 7 分钟
原文
文章信息与版本

文章信息

发布
2025年12月20日
收录
2025年12月20日
更新
2026年11月13日
篇幅
约 7 分钟
获赞
4
题目
P12368

版本

  1. v2当前
  2. v1

看到本题,可以发现核心是模拟删除的过程,且需要高效处理。
注意到有删除操作,需要快速获取删除后每个节点的左右值,考虑使用双向链表。

于是我们可以想到朴素做法:
每次枚举整个字符串,按题意模拟即可。
当进行一次遍历后,字符串没有发生任何改变时,结束程序并输出。
注意要先记录需要删除的下表,在遍历之后一起删除,否则会导致死循环等问题。
代码如下:

C++
#include<bits/stdc++.h>
#define debug() cout<<"_Liyx_ is tangwang"<<endl
#define endl '\n'
using namespace std;

const int N = 1e6 + 10, non = -1;
struct node{
	int pre, nxt;
	char val;
	bool f;
}a[N];

int n, hd;
int res, ending, cnt;
string s;

void add(char v)
{
	cnt ++;
	a[cnt] = {ending, non, v, false};
	a[ending].nxt = cnt;
	ending = cnt;
}

void delet(int x)
{
	int del = x;
	a[a[del].nxt].pre = a[del].pre;
	a[a[del].pre].nxt = a[del].nxt;
	if(a[del].nxt == non) ending = a[del].pre;
}

bool flag = true;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	
	cin >> s;
	n = s.length();
	for(int i = 0; i < n; i ++) add(s[i]);
	
	do{
		flag = false;
		for(int i = a[hd].nxt; i != non; i = a[i].nxt)
		{
			if(a[i].pre == hd || a[i].nxt == non) continue;
			if((a[i].val != a[a[i].pre].val && a[i].val == a[a[i].nxt].val) || (a[i].val == a[a[i].pre].val && a[i].val != a[a[i].nxt].val))
			{
				if(a[i].val == a[a[i].pre].val) a[a[i].nxt].f = true;
				else a[a[i].pre].f = true;
				a[i].f = true;
				flag = true;
			}
		}
		for(int i = a[hd].nxt; i != non; i = a[i].nxt)
		{
			if(a[i].f == true)
			{
				int z = i;
				i = a[i].pre;
				delet(z);
			}
		}
	}while(flag);
	
	if(non == a[hd].nxt) cout << "EMPTY" << endl;
	else for(int i = a[hd].nxt; i != non; i = a[i].nxt) cout << a[i].val;
	
	return 0;
}

接下来我们考虑优化。
对于每一个字符,它这一轮可能被删除,当且仅当它的左右字符中至少有一个在上一轮发生了改变。
所以我们在这一轮遍历的时候记录下一轮需要被遍历的下标即可。
代码:

C++
#include<bits/stdc++.h>
#define debug() cout<<"_Liyx_ is tangwang"<<endl
//#define endl '\n'
using namespace std;
								
const int N = 1e6 + 10, non = -1;
struct node{
	int pre, nxt;
	char val;
	bool f;
}a[N];
								
int n, hd;
int res, ending, cnt;
string s;
								
void add(char v)
{
	cnt ++;
	a[cnt] = {ending, non, v, false};
	a[ending].nxt = cnt;
	ending = cnt;
}

void delet(int x)
{
	int del = x;
	if(a[del].pre != non) a[a[del].pre].nxt = a[del].nxt;
	else hd = a[del].nxt; // 如果删除的是第一个有效节点,更新头指针
	if(a[del].nxt != non) a[a[del].nxt].pre = a[del].pre;
	else ending = a[del].pre;  // 如果删除的是最后一个节点,更新尾指针
	a[x].pre = a[x].nxt = non;
}

bool flag = true;

vector<int> v; // 这一轮需要删除的
set<int> neednow, neednxt;//分别存储这一轮需要遍历的,下一轮需要遍历的

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	
	cin >> s;
	n = s.length();
	for(int i = 0; i < n; i ++)
	{
		add(s[i]);
		neednow.insert(cnt); // 第一次需要遍历所有字符
	}
	do{
		v.clear();
		neednxt.clear();
		flag = false;
		for(int i : neednow)
		{
			if(a[i].pre == hd || a[i].nxt == non) continue;
			if((a[i].val != a[a[i].pre].val && a[i].val == a[a[i].nxt].val) || (a[i].val == a[a[i].pre].val && a[i].val != a[a[i].nxt].val))
			{
				if(a[i].val == a[a[i].pre].val) v.push_back(a[i].nxt);
				else v.push_back(a[i].pre);
				v.push_back(i);
				flag = true;
			}
		}
		for(int nw : v)
		{
			if(a[nw].f == true) continue; // 可能有重复,只处理没有被删除的
			a[nw].f = true;
			neednxt.insert(a[nw].pre);neednxt.insert(a[nw].nxt);
			delet(nw);
		}
		neednow = neednxt;
	}while(flag);
	
	if(non == a[hd].nxt) cout << "EMPTY" << endl;
	else for(int i = a[hd].nxt; i != non; i = a[i].nxt) cout << a[i].val;
	
	return 0;
}
原文
存档评论6 条抓取于