AtCoder AtCoder Beginner Contest 076 C: Dubious Document 2(set与字典序)

Problem Statement

E869120 found a chest which is likely to contain treasure.
However, the chest is locked. In order to open it, he needs to enter a string S consisting of lowercase English letters.
He also found a string S', which turns out to be the string S with some of its letters (possibly all or none) replaced with ?.

One more thing he found is a sheet of paper with the following facts written on it:

  • Condition 1: The string S contains a string T as a contiguous substring.
  • Condition 2: S is the lexicographically smallest string among the ones that satisfy Condition 1.

Print the string S.
If such a string does not exist, print UNRESTORABLE.

Constraints

  • 1|S'|,|T|50
  • S' consists of lowercase English letters and ?.
  • T consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T'

Output

Print the string S.
If such a string does not exist, print UNRESTORABLE instead.

Sample Input 1

Copy
?tc????
coder

Sample Output 1

Copy
atcoder
There are 26 strings that satisfy Condition 1: atcoderbtcoderctcoder,..., ztcoder. Among them, the lexicographically smallest is atcoder, so we can say S=atcoder.


Sample Input 2

Copy
??p??d??
abc

Sample Output 2

Copy
UNRESTORABLE

There is no string that satisfies Condition 1, so the string S does not exist.

题意:给定S,T 2个字符串,判断T字符串是否是S的子串,'?'可以代替任何字符,如果存在,输出S的最小字典序的字符串,否则输出UNRESTORABL思路:利用set自动排序的特性,构造出多个T是S子串的字符串,然后挑选字典序最小的即可,(之前我是想用KMP算法,直接判断是否存在子串,然后再进行变化,不过事实证明这样的算法可行性太低,但是自身学习了下KMP算法也不错)

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<map>
#include<set>
using namespace std;
string s, t;
set<string> S;

void Work()
{
    int lens = s.size();
    int lent = t.size();
    for(int i = 0; i < lens; i++)
    {
        if(s[i] == '?' || s[i] == t[0])//如果字符相等
        {
            string tmp = s;              //构造判断子串之前的字符
            for(int j = 0; j < i; j++)
                if(tmp[j] == '?')
                    tmp[j] ='a';    
            bool can = false;
            for(int j = 0; j < lent; j++)    //判断是否为子串
            {

                if(tmp[i+j]!= '?' && tmp[i+j] != t[j])  //字符匹配
                    break;
                tmp[i+j] = t[j];
                if(j == lent - 1) can = true;
            }
            if(can)
            {
                for(int j = i + lent; j < lens; j++) if(tmp[j] == '?') tmp[j] = 'a';//处理匹配后的字符
                S.insert(tmp);  //压入容器
            }
        }
    }
    if(S.size() == 0) cout << "UNRESTORABLE" << endl;
    else cout << *S.begin() << endl;
}
int main()
{
    cin >> s >> t;
    Work();
}
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值