题目描述
我们定义字符串包含关系:字符串A=abc,字符串B=ab,字符串C=ac,则说A包含B,A和C没有包含关系。
输入描述:
两个字符串,判断这个两个字符串是否具有包含关系,测试数据有多组,请用循环读入。
输出描述:
如果包含输出1,否则输出0.
示例1
输入
复制
abc ab
输出
复制
1
这题测试同样的例子,自测输出正确,提交就错,换到VS上面也能过,什么毛病。
私以为使用find()函数有点作弊啊,
string s1 = "asdfsdfsdfsdf";
string s2 = "abc";
int ans = s1.find(s2) ; //在S1中查找子串S2
返回结果为-1为查找失败,若查找成功则返回子串s2在s1中的第一个下标。
代码
#include<iostream>
#include<string>
using namespace std;
int main() {
int i, temp = -1, j = 0,temp2=-2;
int len1, len2, count = 0;
bool collepse = false;//如果子串出现,则为真,退出循环
bool same = false;
string str1, str2;
while (cin >> str1 >> str2)
{
len1 = str1.length();
len2 = str2.length();
for (i = 0, j = 0; i < len1; i++)
{
// cout <<i<<","<< str1[i] << "," << str2[j] <<",len1:" <<len1<<",len2:"<<len2<<endl;
if (str1[i] == str2[j])
{
temp = i;
count++;
same = true;
j++;
temp2 = i;
if ((count == len1) || (count == len2))
{
collepse = true; break;
}
}
else {
count = 0;
if (same)
{
i--;
same = false;
}
j = 0;
}
}
if (collepse)
{
cout << 1 << endl;
}
else
cout << 0 << endl;
}
return 0;
}