Perfect Keyboard CodeForces - 1303C(思维)

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 2626 lowercase Latin letters will be arranged in some order.

Polycarp uses the same password ss 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 ss, 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 ss, 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 TT (1≤T≤10001≤T≤1000) — the number of test cases.

Then TT lines follow, each containing one string ss (1≤|s|≤2001≤|s|≤200) representing the test case. ss consists of lowercase Latin letters only. There are no two adjacent equal characters in ss.

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 2626 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
思路:按照每个字母左右两边的字母建一个无向图,如果这个图有环的话,就不行。简而言之就是一棵树,但是这棵说不能有度数大于2的点,因为一个字母旁最多只能有两个字母。
代码如下:(代码写的比较丑)

#include
#define ll long long
using namespace std;

map<pair<char,char>,int> mp;
string s;
string tt="abcdefghijklmnopqrstuvwxyz";
int vis[30];
vector<int> ss[30];

inline void init()
{
	mp.clear();
	for(int i=0;i<26;i++) ss[i].clear(),vis[i]=0;
}
inline void dfs(int u,string &S,int f,int &flag)
{
	if(vis[u]==1) return ;
	vis[u]=1;
	S+=(char)(u+'a');
	for(int i=0;i<ss[u].size();i++)
	{
		if(ss[u][i]==f) continue;
		dfs(ss[u][i],S,u,flag);
	}
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		init();
		cin>>s;
		if(s.length()<2)
		{
			cout<<"YES"<<endl<<s;
			for(int i=0;i<tt.length();i++) if(tt[i]!=s[0]) cout<<tt[i];
			cout<<endl;
			continue;
		}
		for(int i=1;i<s.length();i++)
		{
			char c=min(s[i],s[i-1]);
			char d=max(s[i],s[i-1]);
			if(mp[make_pair(c,d)]==0)
			{
				mp[make_pair(c,d)]=1;
				ss[c-'a'].push_back(d-'a');
				ss[d-'a'].push_back(c-'a');
			}
		}
		string x="";
		int flag=1;
		for(int i=0;i<26;i++)
		{
			if(ss[i].size()==1)
			{
				dfs(i,x,-1,flag);
				break;
			}
		}
		for(int i=0;i<26;i++)
		{
			if(ss[i].size()>2) 
			{
				flag=0;
				break;
			}
		}
		int num=0;
		for(int i=0;i<26;i++) num+=vis[i];
		if(x.length()==0||flag==0||x.length()!=num) cout<<"NO"<<endl;
		else 
		{
			cout<<"YES"<<endl<<x;
			for(int i=0;i<26;i++) if(vis[i]==0) cout<<(char)(i+'a');
			cout<<endl;
		}
	}
	return 0;
}

努力加油a啊,(o)/~

你可能感兴趣的:(Perfect Keyboard CodeForces - 1303C(思维))