题解:Codeforces Round 964 (Div. 4) D

D. Slavic’s Exam

time limit per test: 2 seconds

memory limit per test: 256 megabytes

input: standard input

output: standard output


Slavic has a very tough exam and needs your help in order to pass it. Here is the question he is struggling with:

There exists a string s s s, which consists of lowercase English letters and possibly zero or more “?”.

Slavic is asked to change each “?” to a lowercase English letter such that string t t t becomes a subsequence (not necessarily continuous) of the string s s s.

Output any such string, or say that it is impossible in case no string that respects the conditions exists.

Input

The first line contains a single integer T T T ( 1 ≤ T ≤ 1 0 4 1 \leq T \leq 10^4 1T104) — the number of test cases.

The first line of each test case contains a single string s s s ( 1 ≤ ∣ s ∣ ≤ 2 ⋅ 1 0 5 1 \leq |s| \leq 2 \cdot 10^5 1s2105, and s s s consists only of lowercase English letters and “?”-s) – the original string you have.

The second line of each test case contains a single string t t t ( 1 ≤ ∣ t ∣ ≤ ∣ s ∣ 1 \leq |t| \leq |s| 1ts, and t t t consists only of lowercase English letters) – the string that should be a subsequence of string s s s.

The sum of ∣ s ∣ |s| s over all test cases doesn’t exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105, where ∣ x ∣ |x| x denotes the length of the string x x x.

Output

For each test case, if no such string exists as described in the statement, output “NO” (without quotes).

Otherwise, output “YES” (without quotes). Then, output one line — the string that respects all conditions.

You can output “YES” and “NO” in any case (for example, strings “yEs”, “yes”, and “Yes” will be recognized as a positive response).

If multiple answers are possible, you can output any of them.

题意

给你两个字符串
第一个字符串内部可能带有问号
第二个字符串长度小于等于第一个字符串
你可以用任意字符替换第一个字符串内的问号
问:第二个字符串能不能成为第一个字符串的子序列

如果可以,请输出YES和第一个字符串(任意合法情况)
如果不行就输出NO

Example

Input
5
?????
xbx
ab??e
abcde
ayy?x
a
ab??e
dac
paiu
mom
Output
YES
xabax
YES
abcde
YES
ayyyx
NO
NO

题解

很典型的贪心题目
主要是遍历字符串去寻找子序列

用两个指针指向两个字符串的开头

  • 假如 第一字符串指针 指向对象内容?
    ?替换成 第二字符串指针 指向内容
    两个指针一起往后移动
  • 假如 第一字符串指针第二字符串指针 指向对象内容相同,
    两个指针一起往后移动
  • 假如 第一字符串指针第二字符串指针 指向对象内容不同
    第一字符串指针往后移动

假如第一字符串遍历完之后

  • 第二字符串指针还没有遍历完
    就输出NO
  • 第二字符串指针已经遍历完
    先输出YES
    再输出第一字符串(剩下的问号替换成任意字母即可)

代码

#include <bits/stdc++.h>
#define int unsigned long long
#define INF 0x3f3f3f3f
#define all(x) x.begin(),x.end()

int t = 1;

void solve() {
    std::string a,b;
    std::cin >> a >> b;
    int l1 = a.size(),l2 = b.size();
    int st1 = 0,st2 = 0;
    while(st1 != l1 && st2 != l2) {
        if(isalpha(a[st1])) {
            if(a[st1] == b[st2]) st1++,st2++;
            else st1 ++;
        }
        else {
            a[st1] = b[st2];
            st1++,st2++;
        }
    }
    if(st2 == l2) {
        std::cout << "YES\n";
        for(int i = 0 ; i < l1 ; i ++) std::cout << (isalpha(a[i]) ? a[i] : 'a');
        std::cout << "\n";
    }
    else std::cout << "NO\n";
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);

    std::cin >> t;
    while(t--) solve();
    return 0;
}

有什么问题/建议/意见
可以在评论区提出!
非常欢迎!

转载自博客https://www.cnblogs.com/jiejiejiang2004/p/18347331
博主已同意,我就是博主

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,Codeforces Round 511 (Div. 1)是一个比赛的名称。然而,引用内容中没有提供与这个比赛相关的具体信息或问题。因此,我无法回答关于Codeforces Round 511 (Div. 1)的问题。如果您有关于这个比赛的具体问题,请提供更多的信息,我将尽力回答。 #### 引用[.reference_title] - *1* [Codeforces Round 860 (Div. 2)题解](https://blog.csdn.net/qq_60653991/article/details/129802687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces Round 867 (Div. 3)(A题到E题)](https://blog.csdn.net/wdgkd/article/details/130370975)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Codeforces Round 872 (Div. 2)(前三道](https://blog.csdn.net/qq_68286180/article/details/130570952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值