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 }
  • 相关阅读:
    equals()与=的区别
    HashTable和HashMap的区别
    shell高级用法——磁盘管理 创建虚拟的磁盘映射到一个文件
    shell妙用之——dd命令合并多个烧录文件为一个flash镜像
    自动解包ROM 文件获取uboot,uboot-spl ,uImage, rootfs.tar.gz 并烧写SD卡
    运用层通过shell脚本直接操控gpio
    shell脚本之位运算+for循环+返回值承接+shell小数运算
    shell函数递归调用实现目录的对比拷贝
    用debootstrip制作debian环境的rootfs
    shell命令的高级使用之---选择性copy
  • 原文地址:https://www.cnblogs.com/pblr/p/4774374.html
Copyright © 2011-2022 走看看