4-1898E - Sofia and Strings
题意:
题解:对于有排序操作且不限次数,最好考虑每次只对两个排序,如果t中的字母在s中的j位置,则s[0,j]之间小于t中字母的字母都要消去,用队列存s中字母的位置,扫描t,每次用s中剩余位置最小的,在消去不可用的即可。
代码:
点击查看代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int,int> pll;
typedef tuple<int,int,int> TP;
const int N = 1e6 + 10;
void solve()
{
int n,m;
string s,t;
cin>>n>>m;
cin>>s>>t;
queue<int> q[30];
for(int i=0;i<n;i++)
{
int w=s[i]-'a';
q[w].push(i);
}
for(int i=0;i<m;i++)
{
int w=t[i]-'a';
if(q[w].empty())
{
cout<<"NO\n";
return ;
}
int d=q[w].front();
q[w].pop();
// cout<<d<<" "<<" "<<w<<" "<<t[i]<<"\n";
for(int j=0;j<w;j++)
{
while(q[j].size()&&q[j].front()<d)
{
q[j].pop();
}
}
}
cout<<"YES\n";
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int t=1;
cin>>t;
while(t--)
{
solve();
}
return 0 ^ 0;
}
本文作者:xxj112
本文链接:https://www.cnblogs.com/xxj112/p/17844752.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步