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<bits/stdc++.h>
#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)/~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值