题解 | #字符串字符匹配#
字符串字符匹配
https://www.nowcoder.com/practice/22fdeb9610ef426f9505e3ab60164c93
// HJ81 字符串字符匹配.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 //判断短字符串S中的所有字符是否在长字符串T中全部出现。 //请注意本题有多组样例输入。 #include<iostream> #include<bits/stdc++.h> #include<unordered_set> using namespace std; bool check(string a, string b) { unordered_set<char>set;//无序哈希表只存储数据没有key for (auto c : b) { set.insert(c);//将b中的所有字母插入到哈希表set中 } for (auto x : a) { if (!set.count(x))//判断a中的字符是否在b中出现过,count函数返回出现的次数如果次数等于0则返回false { return false; } } return true; /*for (int i = 0; i < a.size(); i++) { for (int j = i; j < b.size(); j++) { if (b.compare(j,a.size(), a, 0,a.size()) == 0)//比较a是否在b中出现(不改变顺序) return true; } } return false;*/ } int main() { string a, b; while (cin>>a>>b) { int m = a.size(); int n = b.size(); if (m > n)swap(a, b);//确保a串始终在前面 if (check(a, b)) cout << "true" << endl; else cout << "false" << endl; } return 0; }