HDU-5202-Rikka with string(DFS + WrongAnswer)

78 篇文章 0 订阅
14 篇文章 0 订阅

Rikka with string

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


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
 

Source
 

Recommend
hujie   |   We have carefully selected several similar problems for you:   5213  5212  5211  5209  5208 

1001 Rikka with string
如果没有不是回文串的限制可以把所有的?都变成a,这样一定是字典序最小的。而现在有了回文串的限制,我们可以从小到大枚举最靠后的不在字符串正中心的问号选什么,其他的问号依然换成a,显然如果有解,这样一定可以得到字典序最小的解。注意特判没有问号以及问号在正中心的情况。
时间复杂度
   
   
    
    O(n)
   
   
Hack点:pretest挺强的也没有什么能hack的了吧

官方说的算法似乎是用的贪心,看起来似乎好理解,但是有个过程我实在是实现不了.....
看完了中二病也不会这道题,好悲哀 哭!
这题博主,还没有AC(博主太水了),学习一下 zzuspy博友的DFS方式......
不管怎样先写个博文先!感谢Acmer zzuspy分享他的源码!



#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

char str[1005];
int len;
int flag;

int judge()                                                  //判断是否是回文数!
{
    for(int i = 0; i <= len / 2; i++)
    {
        if(str[i] != str[len - i - 1]) return 1;
    }
    return 0;
}

void dfs(int x)                                                        //DFS!
{
    if(flag == 0) return;                                             //flag=0,即递归出口已到
    if(x == len)
    {
        if(judge()) 						     //如果不为回文数!
        {
            printf("%s\n", str);
            flag = 0;
        }
        return;
    }
    if(str[x] <= 'z' && str[x] >= 'a')                                //不是'?'往下一位搜索
    {
        dfs(x + 1);
        return;
    }

    if(str[x] == '?')
    {
        for(int i = 'a'; i <= 'z'; i++)                               //如果是'?'则按照字典序来全部替换
        {
            str[x] = (char)i;                                   
            dfs(x + 1);                                               //替换之后继续往下一位深搜
            str[x] = '?'; 		                              //因为每一次只判断一个问号,所以深搜完后要还原!
        }
    }
}

int main()
{
    int n;
    while(scanf("%d", &n) != EOF)
    {
        scanf("%s", str);
        len = strlen(str);
        flag = 1;
        dfs(0);
        if(flag)
        {
            printf("QwQ\n");
        }
    }
    return 0;
}



 



中文题意如下:

Rikka with string

 
 Accepts: 395
 
 Submissions: 2281
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 65536/65536 K (Java/Others)
问题描述
众所周知,萌萌哒六花不擅长数学,所以勇太给了她一些数学问题做练习,其中有一道是这样的:
有一天勇太得到了一个长度为
    
    
     
     n
    
    的字符串,但是六花一不小心把这个字符串搞丢了。于是他们想要复原这一个字符串。勇太记得这个字符串只包含小写字母而且这个串不是回文串。然而不幸的是他已经不记得这个字符串中的一些字符了,你可以帮他复原这个字符串吗?
当然,这个问题对于萌萌哒六花来说实在是太难了,你可以帮帮她吗?
输入描述
多组数据,数据组数不超过
    
    
     
     20
    
    ,每组数据第一行两个正整数
    
    
     
     n
    
    。接下来一行一个长度为
    
    
     
     n
    
    的只包含小写字母和’?’的字符串,’?’表示勇太已经忘了这一个位置的字符了。

    
    
     
     1n103
    
    
输出描述
每组数据输出仅一行一个长度为n的仅包含小写字母的字符串,如果有多种合法解,请输出字典序最小的,如果无解,请输出”QwQ”
输入样例
5
a?bb?
3
aaa
输出样例
aabba
QwQ



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值