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 ...
随机推荐
- 使用pdf.js显示pdf文件
<script type="text/javascript" src="build/pdf.min.js"></script> < ...
- Cheapest Palindrome
这个区间dp解的话是先知道小区间再推大区间,具体需要分类讨论当小区间已经是回文串了,下一层判断,所以一层一个呢还是一层两个呢, 下面讨论一层一个的话是什么情况,那么如果一层两个,可以在评论区写下代码供 ...
- Permission denied 解决办法
Permission denied 解决的办法: $ sudo chmod -R 777 某一目录 其中-R 是指级联应用到目录里的所有子目录和文件777 是所有用户都拥有最高权限
- opencv:图像轮廓发现
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...
- buuctf 基础破解
首先下载压缩包 解压之后发现是另一个压缩包 另一个压缩包上写着基础破解 然后用暴力破解的方法破解 然后搜了一下 暴力破解一般的长度是多少 然后搜到的结果是 长度为8的时候电脑都要破解好几年 所以我猜测 ...
- Linux实现树莓派3B的国密SM9算法交叉编译——(二)miracl库的测试与静态库的生成
先参考这篇文章 Linux实现树莓派3B的国密SM9算法交叉编译——(一)环境部署.简单测试与eclipse工程项目测试 部署好环境,并简单测试交叉编译环境是否安装成功,最后实现在Eclipse上进行 ...
- Linux shell tr 命令详解
该随笔摘自 https://www.jb51.net/article/103892.htm Linux shell tr 命令详解 1. 用途 tr,translate的简写,主要用于压缩重复字符,删 ...
- 本地mongodb数据库导出到远程数据库中
把本地Mongodb中的数据导入(批量插入)到服务器的数据库中 1.导出数据: mongoexport -d admin -c users -o outdatafile.dat 选项解释: -d 指明 ...
- linux sftp 和scp 运用
Linux scp命令: Upload to remote :复制本地文件到远程 Part1: Scp -P port local_file remote_user@remote_ip:rem ...
- Python学习之Craps赌博游戏篇
在此先安利一波大佬的Python学习项目地址:https://github.com/jackfrued/Python-100-Days 这些天一直在看着大佬的项目学习Python,这是第五天循环学习完 ...