#include <iostream> #include <map> using namespace std; int main() { string str1, str2; cin >> str1 >> str2; int len1 = str1.length(); int len2 = str2.length(); map<char, int> mymap; bool bisAllExist = true; for(int i = 0; i < len2; ++i){ mymap[str2[i]]++; } for(int i = 0; i < len1; ++i){ if(mymap[str1[i]] == 0){ bisAllExist = false; break; } } if(bisAllExist){ cout << "true" << endl; }else{ cout << "false" << endl; } } // 64 位输出请用 printf("%lld")
也可以遍历字符串s2,用哈希表记录有哪些字符出现过,然后遍历字符串s1的每个字符,如果能够在哈希表中找到这个字符,说明它在s2中出现过,如果全部找到说明是true,只要有一次找不到就是false。