• B. Complete the Word(Codeforces Round #372 (Div. 2)) 尺取大法


    B. Complete the Word
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    ZS the Coder loves to read the dictionary. He thinks that a word is nice if there exists a substring (contiguous segment of letters) of it of length 26 where each letter of English alphabet appears exactly once. In particular, if the string has length strictly less than26, no such substring exists and thus it is not nice.

    Now, ZS the Coder tells you a word, where some of its letters are missing as he forgot them. He wants to determine if it is possible to fill in the missing letters so that the resulting word is nice. If it is possible, he needs you to find an example of such a word as well. Can you help him?

    Input

    The first and only line of the input contains a single string s (1 ≤ |s| ≤ 50 000), the word that ZS the Coder remembers. Each character of the string is the uppercase letter of English alphabet ('A'-'Z') or is a question mark ('?'), where the question marks denotes the letters that ZS the Coder can't remember.

    Output

    If there is no way to replace all the question marks with uppercase letters such that the resulting word is nice, then print  - 1 in the only line.

    Otherwise, print a string which denotes a possible nice word that ZS the Coder learned. This string should match the string from the input, except for the question marks replaced with uppercase English letters.

    If there are multiple solutions, you may print any of them.

    Examples
    Input
    ABC??FGHIJK???OPQR?TUVWXY?
    Output
    ABCDEFGHIJKLMNOPQRZTUVWXYS
    Input
    WELCOMETOCODEFORCESROUNDTHREEHUNDREDANDSEVENTYTWO
    Output
    -1
    Input
    ??????????????????????????
    Output
    MNBVCXZLKJHGFDSAQPWOEIRUYT
    Input
    AABCDEFGHIJKLMNOPQRSTUVW??M
    Output
    -1
    Note

    In the first sample case, ABCDEFGHIJKLMNOPQRZTUVWXYS is a valid answer beacuse it contains a substring of length26 (the whole string in this case) which contains all the letters of the English alphabet exactly once. Note that there are many possible solutions, such asABCDEFGHIJKLMNOPQRSTUVWXYZ or ABCEDFGHIJKLMNOPQRZTUVWXYS.

    In the second sample case, there are no missing letters. In addition, the given string does not have a substring of length26 that contains all the letters of the alphabet, so the answer is  - 1.

    In the third sample case, any string of length 26 that contains all letters of the English alphabet fits as an answer.

     

    传送门

    题目大意:把所给的字符串中的“?”用A->Z来代替;如果能则输出替换之后的字符串,不能就输出-1.

    思路:运用尺取法每次截取一个长度为26的子字符串

    代码如下:

     1 #include <cstdio>
     2 #include <cstring>
     3 
     4 const int Max=5e4+5;
     5 char S[Max];
     6 int P[30];
     7 
     8 int main(){
     9     while(~scanf("%s",S)){
    10         bool flag=true;
    11         int len=strlen(S);
    12         if(len<26){                //长度短于26的直接pass;
    13             printf("-1
    ");
    14             continue;
    15         }
    16         for(int i=0;i<=len-26 && flag;i++){
    17             int cnt1=0,cnt2=0;
    18             memset(P,0,sizeof(P));
    19             for(int j=i;j<i+26;j++){        //尺取大法;
    20                 if(S[j]>='A'&&S[j]<='Z'){
    21                     P[S[j]-'A']++;      //记录该子字符串里面有多少个字母;
    22                 }
    23                 else{
    24                     cnt2++;        //记录该子字符串里有多少个?;
    25                 }
    26             }
    27             for(int j=0;j<26;j++){
    28                 if(P[j]==1) cnt1++;     //记录子字符串里每个字母出现的次数;
    29             }
    30             if(cnt1+cnt2 == 26){        //判断是否能有一个子字符串能够满足题意;
    31                 int t=0;
    32                 for(int j=i;j<i+26;j++){      //将符合条件的子字符串中的?号换成字母;
    33                     if(S[j]=='?'){
    34                         for(;t<26;t++){
    35                             if(P[t]==0){
    36                                 S[j]='A'+t;
    37                                 t++;
    38                                 break;
    39                             }
    40                         }
    41                     }
    42                 }
    43                 flag=false;     //只需一组即可;
    44             }
    45         }
    46         for(int i=0;i<len;i++){     //剩余的问号随便用字母代替;
    47             if(S[i]=='?'){
    48                 S[i]='A';
    49             }
    50         }
    51         if(flag) printf("-1
    ");
    52         else printf("%s
    ",S);
    53     }
    54 }
    版权声明:本文允许转载,转载时请注明原博客链接,谢谢~
  • 相关阅读:
    java泛型详解
    一条sql语句搞定基于mysql的sql执行顺序的基本理解
    java深拷贝和浅拷贝
    Intelligent idea高效实用总结
    git工具的使用总结
    java注解详解
    实现业务系统中的用户权限管理设计篇
    有创业兴趣的来看看
    System.Web.HttpRequestValidationException:解决方法
    XMLHttpRequest 对象
  • 原文地址:https://www.cnblogs.com/Dillonh/p/8490007.html
Copyright © 2020-2023  润新知