首页 > 其他分享 >题解:Codeforces Round 964 (Div. 4) D

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

时间:2024-08-07 16:43:38浏览次数:7  
标签:964 string 题解 st1 字符串 output Div YES 指针

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\), 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\) becomes a subsequence (not necessarily continuous) of the string \(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\) (\(1 \leq T \leq 10^4\)) — the number of test cases.

The first line of each test case contains a single string \(s\) (\(1 \leq |s| \leq 2 \cdot 10^5\), and \(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\) (\(1 \leq |t| \leq |s|\), and \(t\) consists only of lowercase English letters)  – the string that should be a subsequence of string \(s\).

The sum of \(|s|\) over all test cases doesn't exceed \(2 \cdot 10^5\), where \(|x|\) denotes the length of the string \(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;
}

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

标签:964,string,题解,st1,字符串,output,Div,YES,指针
From: https://www.cnblogs.com/jiejiejiang2004/p/18347331

相关文章

  • 数列 题解
    题目id:1753题目描述给你一个长度为\(N\)的正整数序列,如果一个连续的子序列,子序列的和能够被K整除,那么就视此子序列合法,求原序列包括多少个合法的连续子序列?对于一个长度为\(8\)的序列,\(K=4\)的情况:\(2,1,2,1,1,2,1,2\)。它的答案为\(6\),子序列是位置\(1->\)位置\(8\),\(2->4\)......
  • 题解:Codeforces Round 964 (Div. 4) C
    C.Showeringtimelimitpertest:2secondsmemorylimitpertest:256megabytesinput:standardinputoutput:standardoutputAsacomputersciencestudent,Alexfacesahardchallenge —showering.Hetriestoshowerdaily,butdespitehisbestefforts......
  • CF1689C题解
    CF1689CInfectedTree题解怎么只有我这个蒟蒻不会DP啊。回归正题……简化题意给定一棵以$1$号节点为根的二叉树,总节点个数为$n$。现在$1$号节点感染了病毒,你在这一回合里可以使病毒所在节点的一个儿子不被感染,而病毒会感染一个不受保护的儿子。询问最多可以有多少不......
  • 烧烤 题解
    题目id:11063题目描述\(Snuke\)有一个烧烤聚会。聚会上,他将制作\(N\)份串烧。$\\\\\\\$一份串烧他有\(2N\)根烤肉钎子,它们都将用于制作串烧。第i个烤肉钎子的长度为Li。此外,他有无限供应的原料。他将原料串在两根烤肉钎子上做成一份串烧。一份串烧可串起的原料的最大......
  • 信创系统问题解决笔记
    本文记述解决信创系统显示问题过程经历,描述遇到的各种问题以及解决方法。问题描述测试反馈,在信创系统上,部分界面字体显示异常,表现为内容越界、文字区域显示为小空格,如下图所示:初步分析是字体原因,具体情况需要更进一步分析。问题复现测试团队的信创测试环境有UOS和Kylin两个......
  • CF1999题解
    题目链接CF1999A解题思路模拟。没了。参考代码/*Tips:你数组开小了吗?你MLE了吗?你觉得是贪心,是不是该想想dp?一个小时没调出来,是不是该考虑换题?打cf不要用umap!!!记住,rating是身外之物。该冲正解时冲正解!Problem:算法:思路:*/#include<bits/stdc++.h>usin......
  • Codeforces Round 964 (Div. 4)
    比赛链接:CodeforcesRound964(Div.4)A思路    水题代码#include<iostream>usingnamespacestd;#definelllonglonginlineintread(void){intx=0,f=1;charch=getchar();while(ch<'0'||ch>'9'){......
  • 2024.8.7 模拟赛题解
    T1过于简单,不必阐述。T2给定一个仅包含\(A\)和\(P\)的字符串,每次操作可以删除相邻的\(AP\)或者\(PP\),问字符串最后的最小长度。$Sol:$为求最值问题,考虑贪心。先给出考场做法。发现若干的\(P\)是一定能合并掉的,于是贪心策略,就是如何最小化\(A\)的个数。对于每个......
  • 题解:Codeforces Round 964 (Div. 4) A
    A.A+BAgain?timelimitpertest:1secondmemorylimitpertest:256megabytesinput:standardinputoutput:standardoutputGivenatwo-digitpositiveinteger\(n\),findthesumofitsdigits.InputThefirstlinecontainsaninteger\(t\)(\(1......
  • 【题解】Solution Set - NOIP2024集训Day1 数据结构
    【题解】SolutionSet-NOIP2024集训Day1数据结构https://www.becoder.com.cn/contest/5429「CF1428F」FruitSequences线段树是可以维护区间最长子段的1。记固定右端点在\(i\),的答案为\(f_i\)。那么:\(a_i=0\),\(f_i=f_{i-1}\);\(a_i=1\),打一个单调栈维护所有的最长子......