Educational Codeforces Round 82 C. Perfect Keyboard
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?
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.
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.
5
ababa
codedoca
abcda
zxzytyz
abcdefghijklmnopqrstuvwxyza
YES
bacdefghijklmnopqrstuvwxyz
YES
edocabfghijklmnpqrstuvwxyz
NO
YES
xzytabcdefghijklmnopqrsuvw
NO
大意就是合理安排键盘顺序,他输密码尽可能想让手指不怎么移动,就需要安排按密码时相邻的字母对应的键必须在相邻的位置,没有用到的字母随意安排。可以用一个pos变量存储“最后操作位置”,即上一次输入密码后手指停在哪个地方。当密码下一位没有出现过,看看pos的左右两边能否容许插入新的字母;如果出现过,看看pos两边是否有这个字母,没有的话输出NO,有的话更新pos。最后如果能妥当安排好密码里出现过的字母的话,把剩下的字母随即插入即可
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
vector<char>v;//vector储存键盘排列
char s[];
scanf("%s",s);
int pos=;//pos是最后操作的位置
int i;
bool vis[]={};//判断有没有出现过
int flag=;
for(i=;i<strlen(s);i++)
{
int c=s[i];
if(i==)//第一个密码字母直接插入v即可
{
v.push_back(c);
vis[c-'a'+]=;//标记为出现过
continue;
}
if(!vis[c-'a'+])//如果当前密码字母没出现过
{
if(pos==v.size()-)//最后操作位置在序列末尾的话 直接插入 更新pos和vis数组即可
{
vis[c-'a'+]=;
v.push_back(c);
pos++;
}
else//不在最后
{
if(pos==)//在序列最前面的话也直接插入即可,注意不需要更新pos
{
vis[c-'a'+]=;
pos=;
v.insert(v.begin(),c);//insert较方便
}
else
{
flag=;//表示不存在符合要求的键盘排解
break;
}
}
}
else//出现过 判断旁边的字母是否是密码该位字母
{
if(pos==(v.size()-))//在末尾
{
if(v[v.size()-]==c)
{
pos--;
continue;
}
else
{
flag=;
break;
}
}
else if(pos==)//在开头
{
if(v[]==c)
{
pos++;
continue;
}
else
{
flag=;
break;
}
}
else //在中间
{
if(v[pos-]==c)
{
pos--;
continue;
}
else if(v[pos+]==c)
{
pos++;
continue;
}
else
{
flag=;
break;
}
}
}
}
if(flag==)
{
cout<<"NO"<<endl;
continue;
}
cout<<"YES"<<endl;
for(i=;i<=;i++)
{
vector<char>::iterator it=std::find(v.begin(),v.end(),i-+'a');//把没出现过的字母插入
if(it==v.end())v.push_back(i-+'a');
}
for(i=;i<v.size();i++)
{
putchar(v[i]);
}
cout<<endl;
}
return ;
}
Educational Codeforces Round 82 C. Perfect Keyboard的更多相关文章
- Educational Codeforces Round 82 (Rated for Div. 2) A-E代码(暂无记录题解)
A. Erasing Zeroes (模拟) #include<bits/stdc++.h> using namespace std; typedef long long ll; ; in ...
- [CF百场计划]#3 Educational Codeforces Round 82 (Rated for Div. 2)
A. Erasing Zeroes Description You are given a string \(s\). Each character is either 0 or 1. You wan ...
- 【题解】Educational Codeforces Round 82
比较菜只有 A ~ E A.Erasing Zeroes 题目描述: 原题面 题目分析: 使得所有的 \(1\) 连续也就是所有的 \(1\) 中间的 \(0\) 全部去掉,也就是可以理解为第一个 \ ...
- Educational Codeforces Round 82 (Rated for Div. 2)
题外话 开始没看懂D题意跳了,发现F题难写又跳回来了.. 语文好差,码力好差 A 判第一个\(1\)跟最后一个\(1\)中\(0\)的个数即可 B 乘乘除除就完事了 C 用并查集判一下联通,每个联通块 ...
- Educational Codeforces Round 82 (Rated for Div. 2)E(DP,序列自动机)
#define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ],t[]; int n,m; ][]; ...
- Educational Codeforces Round 82 (Rated for Div. 2)D(模拟)
从低位到高位枚举,当前位没有就去高位找到有的将其一步步拆分,当前位多余的合并到更高一位 #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h&g ...
- Educational Codeforces Round 82 B. National Project
Your company was appointed to lay new asphalt on the highway of length nn. You know that every day y ...
- Educational Codeforces Round 82 A. Erasing Zeroes
You are given a string ss. Each character is either 0 or 1. You want all 1's in the string to form a ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
随机推荐
- Linux jpeglib库的安装
tar -zxvf jpegsrc.v9.tar.gz cd jpeg9 ./configure --enable-shared --enable-static 分别对动态链接库和静态链接库的支持 ...
- 安装k8s出现 Failed to list *api.Node: Get http://192.168.144.131:8080...: dial tcp 192.168.144.131:8080: getsockopt: no route to
原因是master主机的防火墙没关,导致无法访问主机的8080端口,解决方法暂时关闭主机上的防火墙. * centos6 : service iptables stop * centos7 : sys ...
- IntelliJ IDEA 2017.3尚硅谷-----查看项目配置
- Java入门学习路线目录索引
原创 Java入门学习路线目录索引 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/One_ ...
- redis安装与基本使用
什么是Redis 什么是NoSQL 介绍Redis之前,先了解下NoSQL (Not noly SQL)不仅仅是SQL 属于非关系型数据库:Redis就属于非关系型数据库 传统的Mysql ,orac ...
- oracle建表时出现“标识符无效”错误
oracle用以下sql语句新建表时,出现"标识符无效"错误: create table users( user_id integer not null, user_name va ...
- plsql 导出oracle数据库
plsql 导出数据库有两个问题,一个是只导出结构,一个是导出表结构加数据这样的,首先人家让我导成sql语句 这不是简单,首先打开PLSQL 一.导出结构 1.然后tools->Export U ...
- html标签的快捷
https://www.jianshu.com/p/8f330e3571ee 一: <ul> <li><a href=""></a> ...
- zabbix-agent不能启动:配置文件出现特殊字符导致
问题如下: 定位查找问题: 命令:journalctl -xe [重点] 图中可见错误信息在zabbix_agent配置文件中的“hostname=”中不能出现特殊字符,我出错的原因是写成了:hos ...
- 前端开发:mock.js的简单应用(生成随机数据,拦截 Ajax 请求)
摘要 在前端开发过程中,后端接口还没有完全开发完成时,前端开发人员就需要学会自己模拟后端接口数据,更快更好的完成开发任务.模拟后端接口数据的js库有很多,今天就简单就简单的分享下mock.js在前端开 ...