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 ...
随机推荐
- Strategic game(无向?)二分图最小点覆盖(Poj1463,Uva1292)
原题链接 此题求二分图的最小点覆盖,数值上等于该二分图的最大匹配.得知此结论可以将图染色,建有向图,然后跑匈牙利/网络流,如下.然而... #include<iostream> #incl ...
- Docker - 构建一个简单的应用镜像
概述 做个简单的可用镜像 背景 之前的镜像, 都是 命令教程 类的 这次我想构建一个 可以用的 简单镜像镜像 1. 环境 os centos7 docker 18.09 docker image ja ...
- Codeforces Round #620 (Div. 2)E(LCA求树上两点最短距离)
LCA求树上两点最短距离,如果a,b之间距离小于等于k并且奇偶性与k相同显然YES:或者可以从a先走到x再走到y再走到b,并且a,x之间距离加b,y之间距离+1小于等于k并且奇偶性与k相同也输出YES ...
- HTML学习(8)超链接
<a href="url">链接文本或图片</a> 可以使用id属性来访问标记的地方,例: <a id="tip">被访问的 ...
- 离线安装requests库
离线安装需要五个 certifi-2019.9.11-py2.py3-none-any.whl chardet-3.0.4-py2.py3-none-any.whl idna-2.8-py2.py3- ...
- leetcode78.子集➕90.子集2
78子集 dfs dfs1: 和全排列的区别就是对于当前考察的索引i,全排列如果不取i,之后还要取i,所以需要一个visited数组用来记录.对于子集问题如果不取i,之后也不必再取i. 单纯递归回溯 ...
- Flask 教程 第二十一章:用户通知
本文翻译自The Flask Mega-Tutorial Part XXI: User Notifications 这是Flask Mega-Tutorial系列的第二十一章,我将添加一个私有消息功能 ...
- 「题解」「BZOJ-4668」冷战
题目 点这里 思路及代码 我们可以使用并查集的按秩合并(但是不要路径压缩). 两个集合被合并起来,连上的边的权值就设为当前时间. 然后我们可以发现,询问 \(j\) 与 \(k\) 何时联通,就是查询 ...
- 046_使用Scanner获得键盘输入 047_控制语句介绍 048_控制语句_if单选择结构 049_ifelse双选择结构 050_ifelseifelse多选择结构
046_使用Scanner获得键盘输入 package test_package;import java.util.Scanner;/** * 测试获得键盘输入 * @author * */publi ...
- 多种方式实现平均分栏布局(有间距)div平分行宽
以下例子基于分四栏+栏间有间距的例子分析 效果图: html代码: <div class="buy-one-buy"> <h3>淘一淘</h3> ...