一、内容
Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him — his keyboard will consist of only one row, where all 26lowercase Latin letters will be arranged in some order.Polycarp uses the same password son all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in s, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in s, so, for example, the password cannot be password (two characters s are adjacent). Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible?
Input
The first line contains one integer T(1≤T≤1000) — the number of test cases.Then Tlines follow, each containing one string s (1≤|s|≤200) representing the test case. s consists of lowercase Latin letters only. There are no two adjacent equal characters in s
Output
For each test case, do the following: if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem);otherwise, print YES (in upper case), and then a string consisting of 26lowercase Latin letters — the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them.
Input
5
ababa
codedoca
abcda
zxzytyz
abcdefghijklmnopqrstuvwxyza
Output
YES
bacdefghijklmnopqrstuvwxyz
YES
edocabfghijklmnpqrstuvwxyz
NO
YES
xzytabcdefghijklmnopqrsuvw
NO
二、思路
- 就简单判断字母是否能连在一起。 可以用图判断,也可以直接用个字符串简单判断
三、代码
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
bool vis[26];
void solve() {
string s; memset(vis, false, sizeof(vis));
cin >> s;
string k(1, s[0]);
int pos = 0; //最开始的位置
vis[k[0] - 'a'] = true;
for (int i = 1; i < s.size(); i++) {
if (vis[s[i] - 'a']) {
if (pos > 0 && k[pos- 1] == s[i]) {
pos--;
} else if (pos < k.size() - 1 && k[pos + 1] == s[i]) {
pos++;
} else {
printf("NO\n"); return;
}
} else {
if (pos == 0) {
//那么往左边加字符
k = s[i] + k;
} else if(pos == k.size() - 1){
k += s[i]; pos++; //只能往后面加字符
} else {
//不能加字符
printf("NO\n"); return;
}
}
vis[s[i] - 'a'] = true;
}
printf("YES\n");
for (int i = 0; i < 26; i++) if (!vis[i]) k += char(i + 'a');
cout << k << endl;
}
int main() {
int t;
cin >> t;
for (int i = 1; i <= t; i++) solve();
return 0;
}
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 205;
int t, g[26][26];
bool vis[26];
char s[N];
void dfs(int u) {
printf("%c", u + 'a');
for (int j = 0; j < 26; j++) {
if (g[u][j] && !vis[j]) {
vis[j] = true;
dfs(j);
}
}
}
int main() {
scanf("%d", &t);
while (t--) {
memset(vis, false, sizeof(vis));
memset(g, 0, sizeof(g));
scanf("%s", s);
int n = strlen(s);
if (n == 1) {
printf("YES\n");
for (int i = 0; i < 26; i++) if (!vis[i]) printf("%c", i + 'a');
printf("\n");
continue;
}
for (int i = 0; i < n - 1; i++) {
int u = s[i] - 'a', v = s[i + 1] - 'a';
if (g[u][v]) continue;
g[u][v] = g[v][u] = 1;
}
//找出若一个点度为3则NO 并找出度为1的
int du1 = -1, du3 = -1;
for (int i = 0; i < 26; i++) {
int du = 0;
for (int j = 0; j < 26; j++) {
if (g[i][j]) du++;
}
if (du >= 3) du3 = i;
else if (du == 1) du1 = i;
}
if (du3 != -1 || du1 == -1) printf("NO\n");
else {
printf("YES\n");
vis[du1] = true; dfs(du1);
for (int i = 0; i < 26; i++) if (!vis[i]) printf("%c", i + 'a');
printf("\n");
}
}
return 0;
}