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[205]; scanf("%s",s); int pos=0;//pos是最后操作的位置 int i; bool vis[27]={0};//判断有没有出现过 int flag=1; for(i=0;i<strlen(s);i++) { int c=s[i]; if(i==0)//第一个密码字母直接插入v即可 { v.push_back(c); vis[c-'a'+1]=1;//标记为出现过 continue; } if(!vis[c-'a'+1])//如果当前密码字母没出现过 { if(pos==v.size()-1)//最后操作位置在序列末尾的话 直接插入 更新pos和vis数组即可 { vis[c-'a'+1]=1; v.push_back(c); pos++; } else//不在最后 { if(pos==0)//在序列最前面的话也直接插入即可,注意不需要更新pos { vis[c-'a'+1]=1; pos=0; v.insert(v.begin(),c);//insert较方便 } else { flag=0;//表示不存在符合要求的键盘排解 break; } } } else//出现过 判断旁边的字母是否是密码该位字母 { if(pos==(v.size()-1))//在末尾 { if(v[v.size()-2]==c) { pos--; continue; } else { flag=0; break; } } else if(pos==0)//在开头 { if(v[1]==c) { pos++; continue; } else { flag=0; break; } } else //在中间 { if(v[pos-1]==c) { pos--; continue; } else if(v[pos+1]==c) { pos++; continue; } else { flag=0; break; } } } } if(flag==0) { cout<<"NO"<<endl; continue; } cout<<"YES"<<endl; for(i=1;i<=26;i++) { vector<char>::iterator it=std::find(v.begin(),v.end(),i-1+'a');//把没出现过的字母插入 if(it==v.end())v.push_back(i-1+'a'); } for(i=0;i<v.size();i++) { putchar(v[i]); } cout<<endl; } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!