洛谷 P5595 【XR-4】歌唱比赛(构造)

传送门


解题思路

不合法的情况只有中间有个地方相等而后面不相等。
换句话说就是中间凭空出现了Z。
其他的随便设数字就行。

AC代码

#include<cstdio>
#include<iostream>
#include<cstring>
#include<iomanip>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=1e6+5;
string s;
int a[maxn],b[maxn];
int n;
int main(){
	ios::sync_with_stdio(false);
	cin>>s;
	n=s.length();
	for(int i=n-1;i>=0;i--){
		if(i<n-1&&s[i]=='Z'&&s[i+1]!='Z'){
			cout<<-1<<endl;
			return 0;
		}
		if(s[i]=='Z') a[i]=b[i]=0;
		else{
			if(s[i]=='Y') a[i]=5,b[i]=6;
			else a[i]=4,b[i]=3;
		}
	}
	for(int i=0;i<n;i++) cout<<a[i];
	cout<<endl;
	for(int i=0;i<n;i++) cout<<b[i];
	cout<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/yinyuqin/p/15321695.html