zoukankan      html  css  js  c++  java
  • 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 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

      dfs:

     1 #include<cstdio>
     2 #include<cstring>
     3 using namespace std;
     4 int n,t,a[1005],flag,v[1005];
     5 char s[1005];
     6 int f()
     7 {
     8     int i;
     9     for (i=0;i<n/2;i++)
    10     if (s[i]!=s[n-i-1]) return 0;
    11     return 1;
    12 }
    13 void dfs(int p)
    14 {
    15    if (flag) return ;
    16    if (p==t)
    17    {
    18        if (!f())
    19        {
    20            flag=1;
    21            printf("%s
    ",s);
    22        }
    23        return ;
    24    }
    25    int i;
    26    for (i='a';i<='z';i++)
    27    {
    28        if (!v[p])
    29        {
    30            v[p]=1;
    31            s[a[p]]=i;
    32            dfs(p+1);
    33            v[p]=0;
    34        }
    35    }
    36 }
    37 int main()
    38 {
    39     int i;
    40     while (~scanf("%d",&n))
    41     {
    42         t=0;
    43         scanf("%s",&s);
    44         for (i=0;i<n;i++)
    45         {
    46             if (s[i]=='?')
    47             a[t++]=i;
    48         }
    49         memset(v,0,sizeof(v));
    50         flag=0;
    51         dfs(0);
    52         if (!flag) printf("QwQ
    ");
    53     }
    54 }


      

     1 #include<cstdio>
     2 #include<cstring>
     3 using namespace std;
     4 char s[1005];
     5 int n;
     6 int f()
     7 {
     8     int i;
     9     for (i=0;i<n/2;i++)
    10     if (s[i]!=s[n-i-1]) return 0;
    11     return 1;
    12 }
    13 int main()
    14 {
    15     int i,a[1005],k;
    16     while (~scanf("%d",&n))
    17     {
    18         k=0;
    19         scanf("%s",&s);
    20         for (i=0;i<n;i++)
    21         {
    22             if (s[i]=='?')
    23             {
    24                 s[i]='a';
    25                 a[k++]=i;
    26             }
    27         }
    28         if (n==0){printf("QwQ
    ");continue;}
    29         if (k==0&&f()) {printf("QwQ
    ");continue;}
    30         if (!f()) {printf("%s
    ",s);continue;}
    31         s[a[k-1]]='b';
    32         if (!f()) {printf("%s
    ",s);continue;}
    33         if (k==1) {printf("QwQ
    ");continue;}
    34         s[a[k-1]]='a';
    35         s[a[k-2]]='b';
    36         printf("%s
    ",s);
    37     }
    38    return 0;
    39 }
  • 相关阅读:
    LDAP2-创建OU创建用户
    GNE: 4行代码实现新闻类网站通用爬虫
    为什么每一个爬虫工程师都应该学习 Kafka
    新闻网页通用抽取器GNEv0.04版更新,支持提取正文图片与源代码
    写了那么久的Python,你应该学会使用yield关键字了
    新闻类网页正文通用抽取器
    为什么Python 3.6以后字典有序并且效率更高?
    为什么你需要少看垃圾博客以及如何在Python里精确地四舍五入
    数据工程师妹子养成手记——数据库篇
    一行js代码识别Selenium+Webdriver及其应对方案
  • 原文地址:https://www.cnblogs.com/pblr/p/4774374.html
Copyright © 2011-2022 走看看