50分求助
查看原帖
50分求助
543608
Eastz楼主2022/11/4 10:02

不知道哪里有问题

代码如下:

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define in read()
inline int read() {
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9') {x=(x<<1)+(x<<3)+ch-48;ch=getchar();}
	return x*f;
}
inline void write(int x) {
	if(x==0) {putchar('0');putchar('\n');return ;}
	char str[30];register int len=0;
	if(x<0) {x=-x;putchar('-');}
	while(x) {str[len++]=x%10+48;x/=10;}
	while(len--) putchar(str[len]);
	putchar('\n');
}
const int inf=0x3f3f3f3f;
const int N=55;
vector<pii> e[N];
int n,st,ans[N*N],cnt,tot,hc,d[N];bool vis[N*N];char s[3];
inline void dfs(int x) {
	for(int i=0;i<e[x].size();i++) {
		pii v=e[x][i];
		if(vis[v.se]||vis[v.se^1]) continue;
		vis[v.se]=true,vis[v.se^1]=true;
		ans[++cnt]=v.fi;dfs(v.fi);
	}
}
int main() {
	n=in;st=hc=53;
	for(int i=1;i<=n;i++) {
		scanf("%s",s+1);
		int u,v;
		if(s[1]>='A'&&s[1]<='Z') u=s[1]-'A'+1;
		else u=s[1]-'a'+27;
		if(s[2]>='A'&&s[2]<='Z') v=s[2]-'A'+1;
		else v=s[2]-'a'+27;
		d[v]++;d[u]++;
		hc=min(hc,min(u,v));
		e[u].pb(pii(v,tot));tot++;
		e[v].pb(pii(u,tot));tot++;
	}
	tot=0; 
	for(int i=1;i<=52;i++)
		if(d[i]&1) tot++,st=min(st,i);
	if(tot!=0&&tot!=2) {puts("No Solution");return 0;}
	for(int i=1;i<=52;i++) sort(e[i].begin(),e[i].end());
	if(st==53) st=hc;
	ans[++cnt]=st;dfs(st);
	if(cnt<n+1) {puts("No Solution");return 0;}
	for(int i=1;i<=cnt;i++) {
		if(ans[i]<=26&&ans[i]>=1) putchar(ans[i]+'A'-1);
		else putchar(ans[i]+'a'-27);
	}
	puts("");
	return 0;
}
2022/11/4 10:02
加载中...