zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    MS MDS系列之MDS层次结构(Hierarchy)
    Tabular系列之问题1:如何利用其他人的账号进行权限测试?
    MS MDS系列之初识MS Master Data Service(微软主数据服务)
    SQL Server系列之SQL Server 2016 中文企业版详细安装步骤(超多图)
    Pivot Table系列之切片器 (Slicer)
    Pivot Table系列之展开/折叠用法 (Expand/Collapse)
    MyBatis逆向工程——Java代码自动生成
    汇智网练习:修改示例代码,使EzApp组件的标题颜色每秒钟随机变化一次
    关于事件模型,js事件绑定和解除的学习
    使用block进行界面之间的反向传值
  • 原文地址:https://www.cnblogs.com/get-an-AC-everyday/p/4425120.html
Copyright © 2011-2022 走看看