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(1≤n≤1000).
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;
}
时间: 2024-08-26 21:38:24

hdu 5202 Rikka with string(模拟)的相关文章

HDU 5202 Rikka with string (水DFS)

Rikka with string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 624    Accepted Submission(s): 243 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation

HDU - 5202 - Rikka with string (DFS)

Rikka with string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 214    Accepted Submission(s): 109 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation

hdu 5202 Rikka with string (dfs )

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

HDU 6086 Rikka with String

Rikka with String http://acm.hdu.edu.cn/showproblem.php?pid=6086 题意: 求一个长度为2L的,包含所给定的n的串,并且满足非对称. 分析: AC自动机+状压dp. 首先给这个n个串,建立AC自动机.然后去枚举长度为L的一个串,就可以知道另一半了. 如果给定的串完全存在于左边或者右边,那么直接往AC自动机加入这个串或者取反后的反串.如果是跨越中间,那么暴力的把所有的串,从中间切开,然后判断是否合法,加入到AC自动机上就行,如果长度枚举

hdoj - 5202 Rikka with string (BestCoder Round #37 ($))

http://acm.hdu.edu.cn/showproblem.php?pid=5202 字符串处理的题,要细心. 给定一个只包含小写字母和问号的字符串,让我们还原出本来的字符串,把问号替换成任意字符,如果有多种可能输出字典序最小的,原字符串不能是回文串. 首先判断有没有非法字符,然后是否包含问号,如果没有包含则判断是否是回文串,满足则表示不能还原 . 否则把所有问号都替换成‘a',但是可能构成回文,然后继续替换,直到不是回文为止. 1 #include <iostream> 2 #inc

HDU - 6086 Rikka with String AC自动机 + dp

HDU - 6086 前缀和后缀分别建AC自动机, 考虑从两端往中间dp dp[ o ][ i ][ j ][ mask ] 表示放了前面和后面o个, 第一个自动机在 i 位置, 第二个自动机在 j 位置, 拥有的目标串的状态是mask的方案数. 对于跨过两端的东西, 我们最后处理就好了. #include<bits/stdc++.h> #define LL long long #define LD long double #define ull unsigned long long #def

Hdu 3887树状数组+模拟栈

题目链接 Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1757    Accepted Submission(s): 582 Problem Description You are given a tree, it’s root is p, and the node is numbered fr

HDU 4831 Scenic Popularity 暴力模拟

Scenic Popularity Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 340    Accepted Submission(s): 110 Problem Description 临近节日,度度熊们最近计划到室外游玩公园,公园内部包括了很多的旅游景点区和休息区,由于旅游景点很热门,导致景点区和休息区都聚集了很多人.所以度度熊

HDU 3973 AC&#39;s String (substr 强行匹配)

AC's String Time Limit: 30000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1127    Accepted Submission(s): 316 Problem Description You are given some words {Wi}. Then our stupid AC will give you a very long