hdu 5202 Rikka with string(模拟)

Rikka with string

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 625    Accepted Submission(s): 244


Problem Description
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:


One day, Yuta got a string which contains n letters but Rikka lost it in accident. Now they want to recover the string. Yuta remembers that the string only contains lowercase letters and it is not a palindrome string. Unfortunately he cannot remember some letters. Can you help him recover the string?


It is too difficult for Rikka. Can you help her?
 

Input
This problem has multi test cases (no more than 20 ). For each test case, The first line contains a number n(1n1000) . The next line contains an n-length string which only contains lowercase letters and ‘?’ – the place which Yuta is not sure.
 

Output
For each test cases print a n-length string – the string you come up with. In the case where more than one string exists, print the lexicographically first one. In the case where no such string exists, output “QwQ”.
 

Sample Input
      
      
5 a?bb? 3 aaa
 

Sample Output
      
      
aabba QwQ

题目大意:给一个字符串,“?”处用字母替换,要求字符串不回文且字典序最小。


代码如下:

#include <cstdio>
#include <cstring>
char a[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
int main()
{
    int i,j,n;
    char s[1005];
    while(scanf("%d",&n)!=EOF)
    {
        scanf("%s",s);
        int ed,p=0,m=strlen(s);//p=1不回文,p=0回文
        m/=2;
        for(i=0;i<m;i++)//前一半字符串的转换
        {
            if(s[i]!='?'&&s[n-i-1]!='?'&&s[i]!=s[n-1-i])//对应位置字母不同一定不是回文
            {
                p=1;
                continue;
            }
            if(!p && s[i]=='?')
            {
                if(s[n-i-1]=='?' && s[i]=='?')  //前面满足回文条件时,...a....b...就是最小字典序且满足不回文条件
                {
                    s[i]='a';
                    s[n-1-i]='b';
                    p=1;
                }
                else 
                {
                    for(j=0;j<26;j++)     //前面填与后面不同的最小字母时,满足回文条件
                        if(a[j]!=s[n-1-i])
                        {
                            s[i]=a[j];
                            p=1;
                            break;
                        }
                }
            }
            if(p&&s[i]=='?')  //满足不回文条件后,后面所有'?'处都为a
                s[i]='a';
        }
        if(n%2&&s[m]=='?')  //如果时奇数长度,中间"?"一定改为a
            s[m]='a';
		if(n%2)        //后一半字符串从后往前判断的结束位置
			ed=m+1;
		else
			ed=m;
        for(i=n-1;i>=ed;i--)
        {
            if(p&&s[i]=='?')
                s[i]='a';
            else if(!p && s[i]=='?')
            {
                for(j=0;j<26;j++)
                    if(a[j]!=s[n-1-i])
                    {
                        s[i]=a[j];
                        p=1;
                        break;
                    }
            }
        }
        if(!p)
            printf("QwQ\n");
        else
            printf("%s\n",s);
    }
    return 0;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值