第一版(0/20)
#include <iostream>
using namespace std;
int main() {
string s, t;
cin >> s;
cin >> t;
int count = 0;
for(char c : s){
if(t.find(c) == string::npos){
cout << false;
break;
}else {
count++;
}
}
if(count == s.size()){
cout << true;
}
return 0;
}
// 64 位输出请用 printf("%lld")
- 检查s中的所有字符,那就是把它里面的字符拆开来一个个看。
- 判断s里的字符c在不在t里,用
t.find(c)
即可。 - 上面的写法有问题,
break
加在了错误的地方,你是要把整个循环走完才能完成判断的,不可以这么早就跳出循环。 - 但是这么说似乎也不对,因为你已经发现了这个字符不在t里,那么理论上可以直接跳出循环了。
- 更简单的写法就是用count计数最后作比较。
第二版(0/20)
#include <iostream>
using namespace std;
int main() {
string s, t;
cin >> s;
cin >> t;
int count = 0;
for(char c : s){
if(t.find(c) != string::npos){
count++;
}else {
break;
}
}
if(count == s.size()){
cout << true;
}else {
cout << false;
}
return 0;
}
// 64 位输出请用 printf("%lld")
- 这一版我把逻辑写得更清晰了,便于检查错误。
- 从头到尾检查s一遍,对每个字符,看它在不在t里,如果在,计数+1,否则直接离开循环。
- 最后的判断,计数和s大小一样,说明每个字符都通过了判断,否则说明有字符没通过。
第三版(AC)
#include <iostream>
using namespace std;
int main() {
string s, t;
cin >> s;
cin >> t;
int count = 0;
for(char c : s){
if(t.find(c) != string::npos){
count++;
}else {
break;
}
}
if(count == s.size()){
cout << "true";
}else {
cout << "false";
}
return 0;
}
// 64 位输出请用 printf("%lld")
- 直接写
cout << true;
输出的是1,写cout << "true"
输出的才是「true」。