Codeforces 716 B. Complete the Word(暴力)——Codeforces Round #372 (Div. 2)

传送门

B. Complete the Word
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 than 26, 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| ≤ 50000), 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 length 26 (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 as ABCDEFGHIJKLMNOPQRSTUVWXYZ or ABCEDFGHIJKLMNOPQRZTUVWXYS.
In the second sample case, there are no missing letters. In addition, the given string does not have a substring of length 26 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.

题目大意:

给一串字符串,其中字符只有大写字母和 , 其中 能够变为任意字符,求是否有连续的 26 个两两不同的大写字母。

解题思路:

这个题目就是一通暴力,万法皆暴力这句话说的是i很正确的,首先我们在外层套一个 0len26 的大循环用来判断是不是有符合条件的,然后用

一个数组 sum , 数组中的 sum[i] 表示的是从 ‘A’ 到 ‘Z’ 第 i 个是不是已经有了,如果有了 sum[i]=1 否则 sum[i] = 0 ,然后再用 一

个数组 tp 保存下标,首先全部设为 1 ,如果 sum[i]=0 的话,就循环判断 tp[k]=1 如果是 1 ,就改变 str , 但是需要用另一个

字符串改变,不能在原数组中改变,最后判断记录的个数是不是 cnt>=26 OK 了。

My Code

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 1e9+5;
const int MAXN = 1e5+5;
const double eps = 1e-7;
const double PI = acos(-1);
using namespace std;

char str[MAXN], ss[MAXN];
int sum[30], tp[MAXN];
int main()
{
    while(~scanf("%s",str)){
        int len = strlen(str), ok = 0;
        memccpy(ss, str, 0, len);
        for(int i=0; i<len-25; i++){
            int cnt = 0, tmp = 0;
            memset(sum, 0, sizeof(sum));
            memset(tp, -1, sizeof(tp));
            for(int j=i,k=0; j<i+26; k++,j++){
                if(str[ j] != '?')
                    sum[str[j]-'A'] = 1;
                else
                    tp[k] = j, tmp++;
            }
            cnt = tmp;
            for(int i=0; i<26; i++)
                if(sum[i])
                    cnt++;
            for(int j=0; j<26; j++){
                if(!sum[j]){
                    for(int k=0; k<26; k++){
                        if(tp[k] != -1){
                            ss[tp[k]] = ('A'+j);
                            tp[k] = -1;
                            break;
                        }
                    }
                }
            }
            if(cnt >= 26){
                ok = 1;
                break;
            }
        }
        if(!ok)
            puts("-1");
        else{
            for(int i=0; i<len; i++){
                if(ss[i] != '?')
                    putchar(ss[i]);
                else
                    putchar('A');
            }
            puts("");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值