• hdoj


    http://acm.hdu.edu.cn/showproblem.php?pid=5202

    字符串处理的题,要细心。

    给定一个只包含小写字母和问号的字符串,让我们还原出本来的字符串,把问号替换成任意字符,如果有多种可能输出字典序最小的,原字符串不能是回文串。

    首先判断有没有非法字符,然后是否包含问号,如果没有包含则判断是否是回文串,满足则表示不能还原 。

    否则把所有问号都替换成‘a',但是可能构成回文,然后继续替换,直到不是回文为止。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cmath>
     4 #include <vector>
     5 #include <cstring>
     6 #include <string>
     7 #include <algorithm>
     8 #include <string>
     9 #include <set>
    10 #include <functional>
    11 #include <numeric>
    12 #include <sstream>
    13 #include <stack>
    14 #include <map>
    15 #include <queue>
    16 
    17 #define CL(arr, val)    memset(arr, val, sizeof(arr))
    18 
    19 #define ll long long
    20 #define inf 0x7f7f7f7f
    21 #define lc l,m,rt<<1
    22 #define rc m + 1,r,rt<<1|1
    23 #define pi acos(-1.0)
    24 
    25 #define L(x)    (x) << 1
    26 #define R(x)    (x) << 1 | 1
    27 #define MID(l, r)   (l + r) >> 1
    28 #define Min(x, y)   (x) < (y) ? (x) : (y)
    29 #define Max(x, y)   (x) < (y) ? (y) : (x)
    30 #define E(x)        (1 << (x))
    31 #define iabs(x)     (x) < 0 ? -(x) : (x)
    32 #define OUT(x)  printf("%I64d
    ", x)
    33 #define lowbit(x)   (x)&(-x)
    34 #define Read()  freopen("a.txt", "r", stdin)
    35 #define Write() freopen("dout.txt", "w", stdout);
    36 #define N 100005
    37 using namespace std;
    38 
    39 bool palindrome(char *str)  
    40 {
    41     int len=strlen(str);
    42     for(int i=0;i<len/2;i++)
    43     {
    44         if(str[i]!=str[len-i-1]) return 0;
    45     }
    46     return 1;
    47 }
    48 char s[1010];
    49 int f[1010];
    50 int main()
    51 {
    52    //Read();
    53     int n;
    54     while(scanf("%d",&n)!=EOF)
    55     {
    56         getchar();
    57         gets(s);
    58         //printf("%s
    ",s);
    59         bool flag=0;
    60         for(int i=0;i<strlen(s);i++)
    61         {
    62             if(!((s[i]>='a'&&s[i]<='z')||s[i]=='?'))  {flag=1;break;}
    63         }
    64         //printf("%d
    ",flag);
    65         if(flag) {printf("QwQ
    ");continue;}
    66         for(int i=0;i<strlen(s);i++)
    67         {
    68             if(s[i]=='?') {flag=1;break;}
    69         }
    70         if(!flag&&palindrome(s)) {printf("QwQ
    ");continue;}
    71         int j=0;
    72         memset(f,0,sizeof(f));
    73         for(int i=0;i<strlen(s);i++)
    74         {
    75             if(s[i]=='?')
    76             {
    77                 s[i]='a';
    78                 f[j++]=i;
    79             }
    80         }
    81         j--;
    82         flag=0;
    83         if(palindrome(s))
    84         {
    85             s[f[j]]='b';   //从后往前替换  注意 a??aa
    86             while(palindrome(s))
    87             {
    88                 if(j==0) {flag=1;break;}
    89                 j--;
    90                 s[f[j]]='b';
    91                 s[f[j+1]]='a';
    92             }
    93         }
    94         if(flag) printf("QwQ
    ");
    95         else
    96         printf("%s
    ",s);
    97     }
    98     return 0;
    99 }
  • 相关阅读:
    git回滚分支版本到指定版本
    java的垃圾回收
    java对象模型
    java内存模型
    偏向锁浅析
    maven打包报错:在类路径或引导类路径中找不到程序包 java.lang
    《microsoft sql server 2008技术内幕 t-sql语言基础》
    《SQL基础教程》
    内连接,外链接(左连接、右连接、全连接),交叉连接大总结+附SQL JOINS图解[转]
    《大型网站技术架构》1.大型网站架构演练
  • 原文地址:https://www.cnblogs.com/nowandforever/p/4419196.html
Copyright © 2020-2023  润新知