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
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

starlet_kiss

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

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

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

打赏作者

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

抵扣说明:

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

余额充值