HDU 5202 Rikka with string

在一次意外中,Yuta丢失了一个包含n个字母的字符串,但他记得这个字符串只包含小写字母且不是回文。为了帮助Rikka恢复这个字符串,我们设计了一个算法来生成符合条件的所有可能的字符串,并输出字典序最小的那个或提示无法恢复。通过实例展示了如何实现这一算法。

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 <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#define LL long long
#define FOR(i, x, y) for(int i=x;i<=y;i++)
using namespace std;
const int MAXN = 1000 + 10;
char s[MAXN];
int n;
int ok;
bool check()
{
	for(int i=0;i<=n/2;i++)
	{
		if(s[i] != s[n-1-i])
			return false;
	}
	return true;
}
void dfs(int x)
{
	if(ok)
		return ;
	if(x >= n)
	{
		if(!check())
		{
			printf("%s\n", s);
			ok = 1;
		}
		return ;
	}
	if(s[x] >= 'a' && s[x] <= 'z')
	{
		dfs(x + 1);
		return ;
	}
	else
	{
		for(char t = 'a'; t <= 'z'; t++)
		{
			s[x] = t;
			dfs(x + 1);
			s[x] = '?';
		}
	}
}
int main()
{
	while(scanf("%d", &n)!=EOF)
	{
		scanf("%s", s);
		ok = 0;
		dfs(0);
		if(!ok) printf("QwQ\n");
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值