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?

Input

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.

Output

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.
Example
Input

 
5
ababa
codedoca
abcda
zxzytyz
abcdefghijklmnopqrstuvwxyza
Output

 
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 ;
}

最新文章

  1. [阅读笔记]Zhang Y. 3D Information Extraction Based on GPU.2010.
  2. Centos6---Fail2ban
  3. JavaScript SetInterval与setTimeout使用方法详解
  4. weblogic热部署问题
  5. RxCache 的代码分析,含缓存时间duration的在代码中改变的自己实现的机制
  6. (转) MFC的入口点与消息循环,消息映射
  7. 开始使用storm
  8. winform正在使用dsoframer迅速&amp;quot;Unable to display the inactive document.Click here to reacitive the document.&amp;quot;
  9. line-height系列——定义和工作原理总结
  10. hibernate 查询方式汇总
  11. Tesseract-OCR4.0识别中文与训练字库实例
  12. maven -Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME
  13. JArray数组转换为DataTable
  14. pycharm上传代码到github
  15. CentOS下rpm命令详解
  16. randrange()和random() 函数
  17. nodejs 箭头函数
  18. Netsharp配置文件
  19. Frosh Week HDU3743(逆序数)
  20. JavaScript 函数使用return返回值

热门文章

  1. 通过FormData对象可以组装一组用 [XMLHttpRequest]发送请求的键/值对,它可以更灵活方便的发送表单数据。
  2. calloc函数的使用和对内存free的认识
  3. chrome headless+selenium+python+(ubuntu 16.04/centos7) 下的实现
  4. JSP读取数据库二进制图片并显示
  5. const在C与C++中的区别
  6. KM算法模板 hdu 2255
  7. 046_使用Scanner获得键盘输入 047_控制语句介绍 048_控制语句_if单选择结构 049_ifelse双选择结构 050_ifelseifelse多选择结构
  8. tomcat安装成功以后进行测试步骤:
  9. 激活4500-X RTU license
  10. 分享Linux系统快速入门法