• hdu.5202.Rikka with string(贪心)


    Rikka with string

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 581    Accepted Submission(s): 227


    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
     
    Source
     
     1 #include<stdio.h>
     2 #include<string.h>
     3 int cnt = 0 ;
     4 char st[1000 + 10] ;
     5 char rst [1000 + 10] ;
     6 int n , vis[1000 + 10];
     7 
     8 void rev (char s[1000 + 10])
     9 {
    10     char tmp[1000 + 10] ;
    11     int k = 0 , i ;
    12     for (i = strlen (st) - 1 ; i >= 0 ; i-- , k ++) {
    13         tmp[k] = st[i] ;
    14     }
    15     tmp[k] = '' ;
    16     strcpy (s , tmp) ;
    17 }
    18 
    19 int main ()
    20 {
    21    // freopen ("a.txt" , "r" , stdin ) ;
    22     while (~ scanf ("%d" , &n)) {
    23         getchar () ;
    24         gets (st) ;
    25         cnt = 0 ;
    26         for (int i = 0 ; st[i] != '' ;i ++) {
    27             if (st[i] == '?') {
    28                 vis[cnt ++] = i ;
    29                 st[i] = 'a' ;
    30             }
    31         }
    32         int i ;
    33         rev (rst) ;
    34         if (strcmp (st , rst)== 0) {
    35             for (i = cnt - 1 ; i >= 0 ; i--) {
    36                 st[vis[i]] = 'b' ;
    37                 rev (rst) ;
    38                 if (strcmp (rst , st)!= 0) {
    39                     break ;
    40                 }
    41                 st[vis[i]] = 'a' ;
    42             }
    43             if (i == -1 ) puts ("QwQ") ;
    44             else puts (st) ;
    45         }
    46         else puts (st) ;
    47     }
    48     return 0 ;
    49 }
    View Code
    如果没有不是回文串的限制可以把所有的?都变成a,这样一定是字典序最小的。而现在有了回文串的限制,我们可以从小到大枚举最靠后的不在字符串正中心的问号选什么,其他的问号依然换成a,显然如果有解,这样一定可以得到字典序最小的解。注意特判没有问号以及问号在正中心的情况。
    时间复杂度O(n)
  • 相关阅读:
    【sybase】You can’t run SELECT INTO in this database的解决办法
    【IDEA】在IDEA中使用@Slf4j报错,找不到log
    【Java并发】线程的顺序执行
    MySQL报错码对照大全 清风徐来
    Java Swing日期控件的使用 清风徐来
    Android6.0使用BaiDu地图SDK动态获取定位权限 清风徐来
    Sublime Text 2学习记录
    Windows Phone开发笔记1:基础使用
    DirectX学习笔记:关于DX Component结构分析
    Windows 8 Metro开发学习笔记1
  • 原文地址:https://www.cnblogs.com/get-an-AC-everyday/p/4425120.html
Copyright © 2020-2023  润新知