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 ...
随机推荐
- Spring Cloud Netflix Eureka【服务治理】
一.简介 二.使用 一.源码分析
- SaltStack自动化软件简介及安装
==================================================================================================== ...
- C#学习笔记之泛型
泛型的作用和约定 提高性能 拆箱和装箱 从值类型转换为引用类型为装箱,把引用类型转换为值类型为拆箱 装箱和拆箱很容易使用,但是性能损失比较大,尤其是遍历许多项的时候. List<T>不使用 ...
- .NTE Core Web API Example
Source from :https://www.codeproject.com/Articles/1260600/Speed-up-ASP-NET-Core-WEB-API-application- ...
- cannot be resolved to a type解决方法!!!
小楼今天在做一个Java项目的时候遇到一个大家经常遇到的问题:XXX cannot be resolved to a type 看到一百多个errors时的时候,小楼也是被吓得赶紧去找度娘. 归纳一下 ...
- SQL过滤条件on和where
在使用left jion时,会生成一张中间的临时表,然后再将这张临时表返回给用户. on和where条件的区别如下:1. on条件是在生成临时表时使用的条件,它不管on中的条件是否为真,都会返回左边表 ...
- next路由跳转监听
next的路由跳转监听事件 { “routeChangeStart”, "beforeHisroryChange" "routeChangeComplete", ...
- Springboot项目创建文件中相对路径问题
Springboot项目创建文件中相对路径问题 原代码: String location = "./src/main/resources/UsersFiles/" + userId ...
- Docker - 周边 - Go Template
概述 docker inspect -f 引出 希望学会后, 可以处理这个命令 问题: 这是啥 疑问 最开始, 我以为是 jsonpath 后来看 命令行的解释, 说叫 go template 但是我 ...
- 洛谷 P3901 数列找不同(莫队)
题目链接:https://www.luogu.com.cn/problem/P3901 这道题简单莫队模板题,然后$add$和$del$分别处理$vis[]$从$0-->1$和从$1--> ...