Educational Codeforces Round 1303C #82 (Rated for Div. 2) C. Perfect Keyboard
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 26 lowercase Latin letters will be arranged in some order.
Polycarp uses the same password 𝑠 on 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 𝑠, 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 𝑠, 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 𝑇 (1≤𝑇≤1000) — the number of test cases.
Then 𝑇 lines follow, each containing one string 𝑠 (1≤|𝑠|≤200) representing the test case. 𝑠 consists of lowercase Latin letters only. There are no two adjacent equal characters in 𝑠.
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 26 lowercase Latin letters — the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them.
Example
input
5
ababa
codedoca
abcda
zxzytyz
abcdefghijklmnopqrstuvwxyza
output
YES
bacdefghijklmnopqrstuvwxyz
YES
edocabfghijklmnpqrstuvwxyz
NO
YES
xzytabcdefghijklmnopqrsuvw
NO
题意说我要在只有一行的键盘上打字,我打下一个字那么下一个字必须在这个字的左边或右边,这个键盘由小写字母组成,每个字母出现仅一次。
/// @author zhaolu
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <sstream>
#include <algorithm>
#define rep(i,a,b) for(int i=a;i<b;++i)
#define repe(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
#define clc(a,b) memset(a,b,sizeof(a))
#define INF (0x3f3f3f3f)
#define MOD (1000000007)
#define MAX (100000)
#define LEN (MAX+10)
typedef long long ll;
using namespace std;
int t,len,mark;
char str[LEN];
int idx[30];
int at[LEN];
char c[LEN];
int j,minn;
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%s",str);
len=(int)strlen(str);
idx[str[0]-'a']=at[minn=j=100]=++mark;
c[100]=str[0];
rep(i,1,len)
{
if(idx[str[i]-'a']==mark)
{
int k=j;
if(at[j-1]==mark)
{
if(str[i]==c[j-1])
minn=min(minn,--j);
}
if(at[j+1]==mark)
{
if(str[i]==c[j+1])
++j;
}
if(k==j)
{
minn=-1;
break;
}
}
else if(at[j+1]!=mark)
{
idx[str[i]-'a']=at[++j]=mark;
c[j]=str[i];
}
else if(at[j-1]!=mark)
{
minn=min(minn,--j);
idx[str[i]-'a']=at[j]=mark;
c[j]=str[i];
}
else
{
minn=-1;
break;
}
}
if(minn==-1) printf("NO\n");
else
{
printf("YES\n");
while(at[minn]==mark)
printf("%c",c[minn++]);
rep(i,0,26) if(idx[i]!=mark)
printf("%c",'a'+i);
printf("\n");
}
}
return 0;
}