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 }
  • 相关阅读:
    PYTHON 中的字符集
    字符集--发展史
    循序渐进Python3(十三) --7--  django之models
    循序渐进Python3(十三) --8--  django之admin
    循序渐进Python3(十三) --6--  cookie和session
    循序渐进Python3(十三) --5-- django请求处理流程
    循序渐进Python3(十三) --4-- django之csrf使用
    循序渐进Python3(十三) --3-- django之form表单(为自动生成的html标签添加样式)
    循序渐进Python3(十三) --2-- django之form表单(自动生成html标签和自定制提示信息)
    循序渐进Python3(十三) --1-- django之form表单
  • 原文地址:https://www.cnblogs.com/pblr/p/4774374.html
Copyright © 2011-2022 走看看