Perfect Keyboard CodeForces - 1303C(사고)

20961 단어
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 abababababa codedoca abcda zxzytyz abcdefghijklmnopqrstuvwz abcdefghijklmnopqrstuvwxyz YES edocabfghijklmnpqrstuvwxyz NO YES xzytabcdefghijklopqrsuvw 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)/~

좋은 웹페이지 즐겨찾기