问题 I: From S To T-----最暴力的方法!!!用题目意思直接做!!!!

该博客探讨了一种字符串操作问题,目标是通过从字符串p中删除并插入字符到字符串s,使得s等于给定的目标字符串t。文章通过示例解释了问题背景,并提供了一个C++解决方案,该方案首先检查s是否为t的子串,然后尝试通过p中的字符来完成匹配。如果最终无法使s等于t,则说明无法达到目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

You are given three strings s, t and p consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.
During each operation you choose any character from p, erase it from p and insert it into string s (you may insert this character anywhere you want: in the beginning of s, in the end or between any two consecutive characters).
For example, if p is aba, and s is de, then the following outcomes are possible (the character we erase from p and insert into s is highlighted):
aba → ba, de → ade;
aba → ba, de → dae;
aba → ba, de → dea;
aba → aa, de → bde;
aba → aa, de → dbe;
aba → aa, de → deb;
aba → ab, de → ade;
aba → ab, de → dae;
aba → ab, de → dea;
Your goal is to perform several (maybe zero) operations so that s becomes equal to t. Please determine whether it is possible.
Note that you have to answer q independent queries.

输入

The first line contains one integer q (1≤q≤100) — the number of queries. Each query is represented by three consecutive lines.
The first line of each query contains the string s (1≤|s|≤100) consisting of lowercase Latin letters.
The second line of each query contains the string t (1≤|t|≤100) consisting of lowercase Latin letters.
The third line of each query contains the string p (1≤|p|≤100) consisting of lowercase Latin letters.

输出

For each query print YES if it is possible to make s equal to t, and NO otherwise.

样例输入

4
ab
acxb
cax
a
aaaa
aaabbcc
a
aaaa
aabbcc
ab
baaa
aaaaa

样例输入

YES
YES
NO
NO

提示

In the first test case there is the following sequence of operation:
s= ab, t= acxb, p= cax;
s= acb, t= acxb, p= ax;
s= acxb, t= acxb, p= a.
In the second test case there is the following sequence of operation:
s= a, t= aaaa, p= aaabbcc;
s= aa, t= aaaa, p= aabbcc;
s= aaa, t= aaaa, p= abbcc;
s= aaaa, t= aaaa, p= bbcc.

一个字符串题目,本来没有想法,在涵大佬的提示下,得到了灵感,get到了自己的暴力枚举法hhhhh

思路都在代码注释

#include<cstdio>
#include<cstring>
#include<vector>
#include<iostream>
using namespace std;
char s[109],t[109],p[109];
int n,lens,lent,lenp,cnt1=0,cnt2=0;
bool vist[109];
bool visp[109];
int main()
{
    scanf("%d",&n);
    while(n--)
    {
        cnt1=0;
        cnt2=0;
        memset(vist,0,sizeof(vist));
        memset(visp,0,sizeof(visp));
        scanf("%s",s);
        scanf("%s",t);
        scanf("%s",p);	//思路:先从字符串t里面从前往后按顺序找s里的字符,若找到的匹配的个数与字符串s的长度不符, 
        lens=strlen(s);	//则说明s不是t一个子串,即存在字符属于s但是不属于t,即s和p怎么操作都有不匹配的地方 
        lent=strlen(t);	//若匹配到的字符个数与s长度相同,则说明s是t的一个子串,那么再从p里面匹配t中没有被标记过的字符, 
        lenp=strlen(p);	//查找可以没有顺序,因为可以随机插入,但是找到一个必须要在两个字符串中都要标记 ,最后两个被标记的和如果等于t的长度,则满足要求
       	int temp=0;	
		for(int i=0;i<lens;i++)
	  	{
			for(int j=temp;j<lent;j++)
			{
				if(t[j]==s[i]&&!vist[j])
				{
					cnt1++;
					vist[j]=true;
					temp=j; 
					break;
				}	
			}	
		} 
       	if(cnt1<lens)
       	{
       		printf("NO\n");
			continue;	
		}
		for(int i=0;i<lenp;i++)
		{
			for(int j=0;j<lent;j++)
			{
				if(!visp[i]&&!vist[j]&&t[j]==p[i])
				{
					visp[i]=true;
					vist[j]=true;
					cnt2++;
					break;
				}
			}
		}
     	if((cnt1+cnt2)!=lent)
            printf("NO\n");
        else
            printf("YES\n");
    }
    return 0;
}

# T617559 「TPOI-5A」Luminescence ## 题目背景 ![](https://cdn.luogu.com.cn/upload/image_hosting/ownsj515.png) (图片来自 Phigros 曲绘,侵删。) ## 题目描述 给定 $n$ 与两个长度为 $n$ 的序列 $a,b$。定义一个 $0\sim n-1$ 的排列 $q$ 是 **魔怔的**,当且仅当: - $\forall 1\le k\le n,\min^k_{i=1}q_i=a_k$。 - $\forall 1\le k\le n,\min^n_{i=k}q_i=b_k$。 定义一个排列 $q$ 的权值为 $\sum_{1\le l\le r\le n}\operatorname{mex}_{l\le i\le r}q_i$,求所有魔怔的排列的权值之和。答案对 $998244353$ 取模。 一个集合 $M$ 的 $\operatorname{mex}(M)$ 定义为小的没有在 $M$ 中出现的自然数。如 $\text{mex}\{1,2,3,4\}=0,\text{mex}\{0,1,3,4\}=2$。 ## 输入格式 本题有多组测试数据。对于每个测试点,先输入一个正整数 $T$,表示数据组数。 对于每一组测试数据,第一行一个正整数 $n$, 第二行输入 $n$ 个整数 $a_1,a_2,\dots,a_n$,第三行 $n$ 个整数 $b_1,b_2,\dots,b_n$。 ## 输出格式 对于每一组测试数据,输出一行一个整数,表示所有魔怔的排列的权值之和。 ## 输入输出样例 #1 ### 输入 #1 ``` 3 4 0 0 0 0 0 1 2 3 4 1 0 0 0 0 0 2 2 4 0 0 0 0 0 1 1 1 ``` ### 输出 #1 ``` 10 11 14 ``` ## 说明/提示 |$\text{Subtask}$|$n\le$|$\sum n\le$|分值| |:-:|:-:|:-:|:-:| |$1$|$8$|$800$|$20$| |$2$|$10^3$|$10^4$|$40$| |$3$|$2\times10^5$|$2\times10^6$|$40$| 对于 $100\%$ 的数据,$1 \le n \le 2 \times 10^5,0 \le a_i,b_i < n, \sum n \le 2 \times 10^6$。 **对于每组数据保证存在至少一个魔怔的排列。** c++,不用vector,注释
最新发布
08-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值