【Codeforces 716B】Complete the Word

题目链接

【题解】

当时竟然用线段树做的这题。。。 遍历每个位置。 看看每个位置开始的26个除了问号的字母有没有重复的。 没有的话就ok。 然后按顺序放每个字母就好

【代码】

#include <iostream>
#include <cstdio>
using namespace std;

string s;
int temp[100];

void _nextAlpha(char &key,int i){
    while (key<='Z' && temp[key-'A']==i) key++;
}

int main(){
    ios::sync_with_stdio(0),cin.tie(0);
    cin >> s;
    int len = s.size();
    for (int i = 0;i < len;i++){
        if (i+26-1>=len) break;
        bool ok = true;
        for (int j = i;j <= i+26-1 && j<len;j++){
            if (s[j]=='?') continue;
            if (temp[s[j]-'A']==(i+1)){
                ok = false;
                break;
            }else{
                temp[s[j]-'A'] = (i+1);
            }
        }
        if (ok){
            char key = 'A';
            _nextAlpha(key,i+1);
            for (int j = i;j <= i+26-1 && j<len;j++){
                if (s[j]=='?'){
                    s[j]=key;
                    temp[key-'A']=i+1;
                    _nextAlpha(key,i+1);
                }
            }
            for (int i = 0;i < len;i++){
                if (s[i]=='?') s[i]='A';
            }
            cout<<s<<endl;
            return 0;
        }
    }
    puts("-1");
	return 0;
}


posted @ 2020-01-29 17:46  AWCXV  阅读(148)  评论(0编辑  收藏  举报