【栈 临项交换】P7915 [CSP-S 2021] 回文|普及+

本文涉及知识点

C++栈

P7915 [CSP-S 2021] 回文

题目描述

给定正整数 n n n 和整数序列 a 1 , a 2 , … , a 2 n a_1, a_2, \ldots, a_{2 n} a1,a2,,a2n,在这 2 n 2 n 2n 个数中, 1 , 2 , … , n 1, 2, \ldots, n 1,2,,n 分别各出现恰好 2 2 2 次。现在进行 2 n 2 n 2n 次操作,目标是创建一个长度同样为 2 n 2 n 2n 的序列 b 1 , b 2 , … , b 2 n b_1, b_2, \ldots, b_{2 n} b1,b2,,b2n,初始时 b b b 为空序列,每次可以进行以下两种操作之一:

  1. 将序列 a a a 的开头元素加到 b b b 的末尾,并从 a a a 中移除。
  2. 将序列 a a a 的末尾元素加到 b b b 的末尾,并从 a a a 中移除。

我们的目的是让 b b b 成为一个回文数列,即令其满足对所有 1 ≤ i ≤ n 1 \le i \le n 1in,有 b i = b 2 n + 1 − i b_i = b_{2 n + 1 - i} bi=b2n+1i。请你判断该目的是否能达成,如果可以,请输出字典序最小的操作方案,具体在【输出格式】中说明。

输入格式

每个测试点包含多组测试数据。

输入的第一行,包含一个整数 T T T,表示测试数据的组数。对于每组测试数据:

第一行,包含一个正整数 n n n
第二行,包含 2 n 2 n 2n 个用空格隔开的整数 a 1 , a 2 , … , a 2 n a_1, a_2, \ldots, a_{2 n} a1,a2,,a2n

输出格式

对每组测试数据输出一行答案。

如果无法生成出回文数列,输出一行 -1,否则输出一行一个长度为 2 n 2 n 2n 的、由字符 LR 构成的字符串(不含空格),其中 L 表示移除开头元素的操作 1,R 表示操作 2。

你需要输出所有方案对应的字符串中字典序最小的一个。

字典序的比较规则如下:长度均为 2 n 2 n 2n 的字符串 s 1 ∼ 2 n s_{1 \sim 2 n} s12n t 1 ∼ 2 n t_{1 \sim 2 n} t12n 字典序小,当且仅当存在下标 1 ≤ k ≤ 2 n 1 \le k \le 2 n 1k2n 使得对于每个 1 ≤ i < k 1 \le i < k 1i<k s i = t i s_i = t_i si=ti s k < t k s_k < t_k sk<tk

输入输出样例 #1

输入 #1

2
5
4 1 2 4 5 3 1 2 3 5
3
3 2 1 2 1 3

输出 #1

LRRLLRRRRL
-1

输入输出样例 #2

输入 #2

见附件中的 palin/palin2.in

输出 #2

见附件中的 palin/palin2.ans

说明/提示

【样例解释 #1】

在第一组数据中,生成的的 b b b 数列是 [ 4 , 5 , 3 , 1 , 2 , 2 , 1 , 3 , 5 , 4 ] [4, 5, 3, 1, 2, 2, 1, 3, 5, 4] [4,5,3,1,2,2,1,3,5,4],可以看出这是一个回文数列。

另一种可能的操作方案是 LRRLLRRRRR,但比答案方案的字典序要大。

【数据范围】

∑ n \sum n n 表示所有 T T T 组测试数据中 n n n 的和。

对所有测试点保证 1 ≤ T ≤ 100 1 \le T \le 100 1T100 1 ≤ n , ∑ n ≤ 5 × 10 5 1 \le n, \sum n \le 5 \times {10}^5 1n,n5×105

测试点编号 T ≤ T \le T n ≤ n \le n ∑ n ≤ \sum n \le n特殊性质
1 ∼ 7 1 \sim 7 17 10 10 10 10 10 10 50 50 50
8 ∼ 10 8 \sim 10 810 100 100 100 20 20 20 1000 1000 1000
11 ∼ 12 11 \sim 12 1112 100 100 100 100 100 100 1000 1000 1000
13 ∼ 15 13 \sim 15 1315 100 100 100 1000 1000 1000 25000 25000 25000
16 ∼ 17 16 \sim 17 1617 1 1 1 5 × 10 5 5 \times {10}^5 5×105 5 × 10 5 5 \times {10}^5 5×105
18 ∼ 20 18 \sim 20 1820 100 100 100 5 × 10 5 5 \times {10}^5 5×105 5 × 10 5 5 \times {10}^5 5×105
21 ∼ 25 21 \sim 25 2125 100 100 100 5 × 10 5 5 \times {10}^5 5×105 5 × 10 5 5 \times {10}^5 5×105

特殊性质:如果我们每次删除 a a a 中两个相邻且相等的数,存在一种方式将序列删空(例如 a = [ 1 , 2 , 2 , 1 ] a = [1, 2, 2, 1] a=[1,2,2,1])。

【hack 数据提供】
@潜在了H2O下面

栈 临项交换

分两种情况:第一次选择L,第一次选择R。我们讨论第一种情况,第二种情况,不赘述。
用双向队列模拟栈,令a[i]=a[0],且 a ≠ 0 a\neq 0 a=0
a[i]是最后一个元素,可以L,也可以R。que1中的元素只能L,que2中的元素只能R。
a[0…i-1]依次加入队列que1,a[i+1…n-1]逆序加入队列que2。当前操作只能在队首选择,最后的操作只能在队尾。由于是回文,故当前数字必须同时是队首和队尾。如果不存在这样的数字则无解。
如果存在两个数,优先选择que1的队首,这样字典序小。选择que1操作是L,选择que2操作是R。如果某合法操作先从que1取出若干数字str1,后从que2取出若干数字str2,则进行临项交换也是回文。 ⋯ s t r 1 s t r 2 ⋯ s t r 2 s t r 1 ⋯ \cdots str1str2\cdots str2 str1 \cdots str1str2str2str1是回文,则 ⋯ s t r 2 s t r 1 ⋯ s t r 1 s t r 2 ⋯ \cdots str2str1\cdots str1 str2 \cdots str2str1str1str2也是回文。
注意:如果一个队列只有一个元素,不能同时当队首、队尾。
is判断que1的队首能否是que1或que2的队尾。
que1.size() > 1且 que1的 队首等于队尾,返回true。
que1.size()>0 que2.size() >0且 que1的 队首等于que2队尾,返回true。
其它返回false
N等于1,可能要做特殊处理。

代码

核心代码

#include <iostream>
#include <sstream>
#include <vector>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<string>
#include<algorithm>
#include<functional>
#include<queue>
#include <stack>
#include<iomanip>
#include<numeric>
#include <math.h>
#include <climits>
#include<assert.h>
#include<cstring>
#include<list>

#include <bitset>
using namespace std;

template<class T1, class T2>
std::istream& operator >> (std::istream& in, pair<T1, T2>& pr) {
	in >> pr.first >> pr.second;
	return in;
}

template<class T1, class T2, class T3 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3>& t) {
	in >> get<0>(t) >> get<1>(t) >> get<2>(t);
	return in;
}

template<class T1, class T2, class T3, class T4 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4>& t) {
	in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t);
	return in;
}

template<class T = int>
vector<T> Read() {
	int n;
	cin >> n;
	vector<T> ret(n);
	for (int i = 0; i < n; i++) {
		cin >> ret[i];
	}
	return ret;
}
template<class T = int>
vector<T> ReadNotNum() {
	vector<T> ret;
	T tmp;
	while (cin >> tmp) {
		ret.emplace_back(tmp);
		if ('\n' == cin.get()) { break; }
	}
	return ret;
}

template<class T = int>
vector<T> Read(int n) {
	vector<T> ret(n);
	for (int i = 0; i < n; i++) {
		cin >> ret[i];
	}
	return ret;
}

template<int N = 1'000'000>
class COutBuff
{
public:
	COutBuff() {
		m_p = puffer;
	}
	template<class T>
	void write(T x) {
		int num[28], sp = 0;
		if (x < 0)
			*m_p++ = '-', x = -x;

		if (!x)
			*m_p++ = 48;

		while (x)
			num[++sp] = x % 10, x /= 10;

		while (sp)
			*m_p++ = num[sp--] + 48;
		AuotToFile();
	}
	void writestr(const char* sz) {
		strcpy(m_p, sz);
		m_p += strlen(sz);
		AuotToFile();
	}
	inline void write(char ch)
	{
		*m_p++ = ch;
		AuotToFile();
	}
	inline void ToFile() {
		fwrite(puffer, 1, m_p - puffer, stdout);
		m_p = puffer;
	}
	~COutBuff() {
		ToFile();
	}
private:
	inline void AuotToFile() {
		if (m_p - puffer > N - 100) {
			ToFile();
		}
	}
	char  puffer[N], * m_p;
};

template<int N = 1'000'000>
class CInBuff
{
public:
	inline CInBuff() {}
	inline CInBuff<N>& operator>>(char& ch) {
		FileToBuf();
		ch = *S++;
		return *this;
	}
	inline CInBuff<N>& operator>>(int& val) {
		FileToBuf();
		int x(0), f(0);
		while (!isdigit(*S))
			f |= (*S++ == '-');
		while (isdigit(*S))
			x = (x << 1) + (x << 3) + (*S++ ^ 48);
		val = f ? -x : x; S++;//忽略空格换行		
		return *this;
	}
	inline CInBuff& operator>>(long long& val) {
		FileToBuf();
		long long x(0); int f(0);
		while (!isdigit(*S))
			f |= (*S++ == '-');
		while (isdigit(*S))
			x = (x << 1) + (x << 3) + (*S++ ^ 48);
		val = f ? -x : x; S++;//忽略空格换行
		return *this;
	}
	template<class T1, class T2>
	inline CInBuff& operator>>(pair<T1, T2>& val) {
		*this >> val.first >> val.second;
		return *this;
	}
	template<class T1, class T2, class T3>
	inline CInBuff& operator>>(tuple<T1, T2, T3>& val) {
		*this >> get<0>(val) >> get<1>(val) >> get<2>(val);
		return *this;
	}
	template<class T1, class T2, class T3, class T4>
	inline CInBuff& operator>>(tuple<T1, T2, T3, T4>& val) {
		*this >> get<0>(val) >> get<1>(val) >> get<2>(val) >> get<3>(val);
		return *this;
	}
	template<class T = int>
	inline CInBuff& operator>>(vector<T>& val) {
		int n;
		*this >> n;
		val.resize(n);
		for (int i = 0; i < n; i++) {
			*this >> val[i];
		}
		return *this;
	}
	template<class T = int>
	vector<T> Read(int n) {
		vector<T> ret(n);
		for (int i = 0; i < n; i++) {
			*this >> ret[i];
		}
		return ret;
	}
	template<class T = int>
	vector<T> Read() {
		vector<T> ret;
		*this >> ret;
		return ret;
	}
private:
	inline void FileToBuf() {
		const int canRead = m_iWritePos - (S - buffer);
		if (canRead >= 100) { return; }
		if (m_bFinish) { return; }
		for (int i = 0; i < canRead; i++)
		{
			buffer[i] = S[i];//memcpy出错			
		}
		m_iWritePos = canRead;
		buffer[m_iWritePos] = 0;
		S = buffer;
		int readCnt = fread(buffer + m_iWritePos, 1, N - m_iWritePos, stdin);
		if (readCnt <= 0) { m_bFinish = true; return; }
		m_iWritePos += readCnt;
		buffer[m_iWritePos] = 0;
		S = buffer;
	}
	int m_iWritePos = 0; bool m_bFinish = false;
	char buffer[N + 10], * S = buffer;
};

class Solution {
public:
	string Ans(const int N, vector<int>& a) {
		deque<int> q1, q2, q3, q4;
		const int inx1 = std::find(a.begin() + 1, a.end(), a[0]) - a.begin();
		int i = 0;
		for (i = 1; i < inx1; i++) { q1.push_back(a[i]); }
		for (i = inx1 + 1; i < a.size(); i++) { q2.push_front(a[i]); }
		string str1 = Do(q1, q2);
		if ("X" != str1) { return "L" + str1 + "L"; }
		const int inx2 = std::find(a.begin(), a.end(), a.back()) - a.begin();
		for (i = 0; i < inx2; i++) { q3.push_back(a[i]); }
		for (i = inx2 + 1; i + 1 < a.size(); i++) { q4.push_front(a[i]); }
		string str2 = Do(q3, q4);
		if ("X" != str2) { return "R" + str2 + "L"; }
		return "";
	}
	int Is(deque<int>& que1, deque<int>& que2) {
		if ((que1.size() > 1) && (que1.front() == que1.back())) {
			que1.pop_front(); que1.pop_back();
			return 1;
		}
		if (que1.size() && que2.size() && (que1.front() == que2.back())) {
			que1.pop_front(); que2.pop_back();
			return 2;
		}

		return 0;
	}
	string Do(deque<int>& que1, deque<int>& que2) {
		string str1, str2;
		while (que1.size() || que2.size()) {
			int ret = Is(que1, que2);
			if (ret > 0) {
				str1 += "L", str2 += ((1 == ret) ? "L" : "R");
				continue;
			}
			ret = Is(que2, que1);
			if (ret > 0) {
				str1 += "R", str2 += ((1 == ret) ? "R" : "L");
				continue;
			}
			return "X";
		}
		return str1 + string(str2.rbegin(), str2.rend());
	}
};


int main() {
#ifdef _DEBUG
	freopen("a.in", "r", stdin);
#endif // DEBUG	
	ios::sync_with_stdio(0); cin.tie(nullptr);
	CInBuff<> in; COutBuff<10'000'000> ob;
	int T;
	in >> T;
	for (int i = 0; i < T; i++) {
		int N;
		in >> N;
		auto a = in.Read(2 * N);
#ifdef _DEBUG		
		printf("N=%d", N);
		Out(a, "a=");
		//Out(ab2, "ab2=");
		//Out(que, "que=");
		//Out(B, "B=");
#endif // DEBUG	
		auto res = Solution().Ans(N, a);
		if ("" == res) { res = "-1"; }
		ob.writestr(res.c_str()); ob.write('\n');
	}
	return 0;
}

扩展阅读

算法为骨,CAD为魂
亲士工具箱:支持中望CAD2024、AutoCad2013及以上,多年承接CAD项目的精华
工作中遇到的问题,可以按类别查阅鄙人的算法文章,请点击《算法与数据汇总》。
学习算法:按章节学习《喜缺全书算法册》,大量的题目和测试用例,打包下载。重视操作
活到老,学到老。明朝中后期,大约50%的进士能当上堂官(副部及更高);能当上堂官的举人只有十余人。
子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。

视频课程

先学简单的课程,请移步CSDN学院,听白银讲师(也就是鄙人)的讲解。
https://edu.csdn.net/course/detail/38771
如何你想快速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程
https://edu.csdn.net/lecturer/6176

测试环境

操作系统:win7 开发环境: VS2019 C++17
或者 操作系统:win10 开发环境: VS2022 C++17
如无特殊说明,本算法用**C++**实现。

评论 53
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

软件架构师何志丹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值