P13491 【MX-X14-T1】拼凑基因

· · 题解

题目传送门

题目大意

两个字符串 st,判断将 s 分解成若干个字串后(注意不是子序列)以任意顺序重新排列字串是否能使其变成 t

思路

可以使用两个 map 来存储两个字符串。如果字符串 t 中某一个字符的数量与字符串 s 中同一字符的数量不一致,那么 s 无论如何都不能变成 t

AC Code:

#include <bits/stdc++.h>
using namespace std;
map<char,int> ss,tt;
int main()
{
    int n;
    cin>>n;
    string s,t;
    cin >>s>>t;
    for(char c:s)
    {
        ss[c]++;
    }
    for(char c:t)
    {
        tt[c]++;
    }
    for(auto x:tt)
    {
        if(tt[x.first]>ss[x.first])
        {
            cout <<"No";
            exit(0);
        }
    }
    cout <<"Yes";
    return 0;
}