#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <string>
#define inf 0xfffffff
#define eps 1e-10
using namespace std;
int n;
char str[1005];
bool flag;
void Input()
{
    flag = false;
    getchar();
    for (int i = 0; i <= n; ++i)
    {
        str[i] = getchar();
    }
}
void Output()
{
    int i = 0;
    while (str[i] != ‘\n‘)
    {
        printf("%c", str[i]);
        i++;
    }
    printf("\n");
}
bool Cheak()
{
    int len = n/2;
    for (int i = 0; i < len; ++i)
    {
        if (str[i] != str[n-1-i])
            return false;
    }
    return true;
}
void dfs(int i)
{
    if (flag)
        return;
    while (str[i] != ‘?‘ && str[i] != ‘\n‘)
        i++;
    if (str[i] == ‘\n‘)
    {
        if (!Cheak())
        {
            flag = true;
        }
        return;
    }
    if (str[i] == ‘?‘)
    {
        for (char j = ‘a‘; j <= ‘z‘; ++j)
        {
            str[i] = j;
            dfs(i+1);
            if (flag)
                return;
            str[i] = ‘?‘;
        }
    }
}
int main()
{
    //freopen("test.txt", "r", stdin);
    while (scanf("%d", &n) != EOF)
    {
        Input();
        dfs(0);
        if (flag)
            Output();
        else
            printf("QwQ\n");
    }
    return 0;
}
ACM学习历程——HDU5202 Rikka with string(dfs,回文字符串)
原文:http://www.cnblogs.com/andyqsmart/p/4430413.html