C. Perfect Keyboard

194 篇文章 1 订阅
5 篇文章 0 订阅

链接:https://codeforces.ml/contest/1303/problem/C

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 sare 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

Copy

5
ababa
codedoca
abcda
zxzytyz
abcdefghijklmnopqrstuvwxyza

output

Copy

YES
bacdefghijklmnopqrstuvwxyz
YES
edocabfghijklmnpqrstuvwxyz
NO
YES
xzytabcdefghijklmnopqrsuvw
NO

代码:

#include<bits/stdc++.h>
using namespace std;
long long t,n;
map<char,bool> flag;
int main()
{
	cin>>t;
	while(t--)
	{
		for(char j='a';j<='z';j++)
		flag[j]=0;
		string s,ans="";
		cin>>s;
		int len=s.length(),k=1,now,flag1=1,len1=0;
		ans+=s[0]; 
		now=0; 
		flag[s[0]]=1;
		for(int j=1;j<len;j++)
		{
			if(flag[s[j]])
			{
				if(ans[now-1]!=s[j]&&ans[now+1]!=s[j])
				{
					flag1=0;
					break;
				}
				if(ans[now-1]==s[j])
				{
					k=-1;
					now-=1;
				}
				else
				{
					k=1;
					now+=1;
				}
			}
			else
			{
				flag[s[j]]=1;
				if(k==-1&&now!=0||k==1&&now!=len1)
				{
					flag1=0;
					break;
				}
				if(k==-1)
				{
					len1++;
					ans=s[j]+ans;
				}
				else
				{
					now++;
					len1=max(len1,now);
					ans+=s[j];
				}
			}
		}	
		if(flag1)
		{
			cout<<"YES\n";
			for(char j='a';j<='z';j++)
			{
				if(!flag[j])
				ans+=j;
			}
			cout<<ans<<endl;
		}
		else
		cout<<"NO\n";
	}
	return 0;
}

 

首先,可以使用 set() 代替 idx 列表,因为我们只需要判断一个元素是否在集合中。这样可以减少重复的判断。 其次,在 User 类的初始化函数中,可以直接将属性设置为默认值,而不是在每个属性上进行赋值。 另外,在循环中,可以使用 enumerate() 函数代替 range() 函数和下标变量,这样可以避免手动维护下标变量。 最后,在计算排名时,可以使用 itertools.groupby() 函数实现,这样可以避免手动维护排名和比较。 下面是优化后的代码: ```python import itertools class User: def __init__(self): self.id = 0 self.perfect = 0 self.rank = 0 self.mk = -1 self.grade = [0, -1, -1, -1, -1, -1] def lt(self, other): if self.grade[0] != other.grade[0]: return self.grade[0] > other.grade[0] elif self.perfect != other.perfect: return self.perfect > other.perfect else: return self.id < other.id if __name__ == "__main__": N, K, M = map(int, input().split()) p = [0] + list(map(int, input().split())) u = [None] * (N + 1) idx = set() for i in range(M): tmp, pid, tmpgrade = map(int, input().split()) if not u[tmp]: u[tmp] = User() u[tmp].id = tmp if tmpgrade != -1: u[tmp].mk = 1 else: tmpgrade = 0 if tmpgrade > u[tmp].grade[pid]: u[tmp].grade[pid] = tmpgrade idx.add(tmp) for i, user in enumerate(u): if user and user.mk > 0: user.grade[0] = sum(user.grade[j] for j in range(1, K + 1) if user.grade[j] >= 0) user.perfect = sum(user.grade[j] == p[j] for j in range(1, K + 1)) al = sorted([user for user in u if user and user.mk > 0], key=lambda x: (-x.grade[0], -x.perfect, x.id)) for rank, group in itertools.groupby(al, key=lambda x: (x.grade[0], x.perfect)): for user in group: user.rank = rank for user in sorted(list(group), key=lambda x: x.id): print("{:d} {:05d}".format(user.rank, user.id), end="") for j in range(1, K + 1): if user.grade[j] >= 0: print(" {:d}".format(user.grade[j]), end="") else: print(" -", end="") print() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值