您的位置:首页 > 其它

[luogu1341]无序字母对

2017-10-25 22:10 190 查看
链接

题目描述

给定n个各不相同的无序字母对(区分大小写,无序即字母对中的两个字母可以位置颠倒)。请构造一个有n+1个字母的字符串使得每个字母对都在这个字符串中出现。

输入输出格式

输入格式:

第一行输入一个正整数n。

以下n行每行两个字母,表示这两个字母需要相邻。

输出格式:

输出满足要求的字符串。

如果没有满足要求的字符串,请输出“No Solution”。

如果有多种方案,请输出前面的字母的ASCII编码尽可能小的(字典序最小)的方案

输入输出样例

输入样例#1:

4

aZ

tZ

Xt

aX

输出样例#1:

XaZtX

说明

【数据规模与约定】

不同的无序字母对个数有限,n的规模可以通过计算得到。

//以前没做过欧拉回路的题

//今天做了个试了试水

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn = 100000 + 100;
int n;
struct edge {
int u,v,w;
int next;
}e[maxn];
int head[maxn], tot = 0;
char str[2];
int a[100][100],d[100],val[maxn];

int read() {
int x = 0, t = 1;
char ch = getchar();
while(ch < '0' || ch >'9'){
if(ch == '-') t = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9'){
x = x * 10 + ch - '0';
ch = getchar();
}
return x * t;
}

void dfs(int x) {
for(int i = 1; i <= 60; i++) {
if(a[x][i]) {
a[x][i] = a[i][x] = 0;
dfs(i);
}
}
val[++tot] = x;
}

int main() {
n = read();
for(int i = 1; i <= n; i++) {
cin>>str;
int l = str[0] - 'A' + 1,r = str[1] - 'A' + 1;
a[l][r] = 1, a[r][l] = 1;
d[l]++, d[r]++;
}
int cnt = 0, u1 = 1<<29,u2 = 1<<29;
//ASCII中 A-Z a-z之间有其他的符号,所以不要到52
for(int i =1; i <= 60; i++) {
if(d[i] & 1) {
cnt++;
u1 = min(u1,i);
}
else if(d[i] != 0) {
u2 = min(u2,i);
}
}
if(cnt != 0 && cnt != 2) {
printf("No Solution\
9c23
n");
return 0;
}
tot = 0;
if(!cnt) dfs(u2);
else dfs(u1);
for(int i = tot; i >= 1; i--) {
char a = val[i] + 'A' - 1;
cout<<a;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: