C. Perfect Keyboard

两种方法

第一种是进行模拟

我们知道他输入字符串的下一个字符一定是与上一个字符处于相邻的位置所以而下一个字符的左边或右边放的就是上一个字符因此对于我们的下一个字符我们只有一个位置可以放,当让也可能下一个字符和上一个字符是相等的所以对于我们的下一个字符我们是可以确定他的位置的于是就有了我们的模拟做法

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1e9+7;
const int INF=0x3f3f3f3f;
const int N = 1e5+10;
int a[30];
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
void solve()
{
    memset(a,0,sizeof(a));
    string s;
    cin>>s;
    int n=s.size();
    s=" "+s;
    string tmp="";
    for(int i=1;i<=66;i++)
    {
        tmp+='#';
    }
    int now=30;
    int f=1;
    for(int i=1;i<=n;i++)
    {
        if(tmp[now]=='#')
        {
            tmp[now]=s[i];
            a[s[i]-'a'+1]++;
        }
        else if(tmp[now-1]==s[i])
        {
            now--;
        }
        else if(tmp[now+1]==s[i])
        {
            now++;
        }
        else if(tmp[now-1]=='#')
        {
            now--;
            tmp[now]=s[i];
            a[s[i]-'a'+1]++;            
        }
        else if(tmp[now+1]=='#')
        {
            now++;
            tmp[now]=s[i];
            a[s[i]-'a'+1]++;            
        }
        else 
        {
            f=0;
            break;
        } 
    }
    if(f==1)
    {
        int cnt=0;
        
        for(int i=0;i<66;i++)
        {
            if(tmp[i]!='#')
            cnt++;
        }
        for(int i=1;i<=26;i++)
        {
            if(a[i]==0)
            {
                cnt++;
            }
        }
            if(cnt>26)
            {
                cout<<"NO"<<endl;
                return ;
            }
            else 
            {
                cout<<"YES"<<endl;
            }
            
            for(int i=0;i<66;i++)
            {
                if(tmp[i]!='#')
                {
                    cout<<tmp[i];
                }
            }
            for(int i=1;i<=26;i++)
            {
                if(a[i]==0)
                {
                    cout<<char('a'+i-1);
                }
            }
            cout<<endl;    
    }
    else
    {
        cout<<"NO"<<endl;
    }
        
}
signed main()
{
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值