问题遇到的现象和发生背景
https://www.luogu.com.cn/problem/P1308
80分,5、9两个数据点一直过不了
问题相关代码,请勿粘贴截图
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
string title, text;
int a=-1, ans=0,flag=1,t;
getline(cin, title);
getline(cin, text);
for (int i = 0; i < title.length(); i++)
{
title[i] = tolower(title[i]);
}
for (int i = 0; i < text.length(); i++)
{
text[i] = tolower(text[i]);
}
title=" "+title+" ";
text=" "+text+" ";
while (text.find(title) != string::npos)
{
t = text.find(title);
if (flag)
{
a = t;
flag = 0;
}
ans++;
text.erase(0,t+1+ title.length());
}
if (a != -1)
cout << ans << " ";
else
;
cout << a <<endl;
//cout << title << endl;
//cout << text << endl;
return 0;
}
运行结果及报错内容
读取数据的时候读到一半就卡住了,第五个数据点正确答案31 163,我的答案11 163
我的解答思路和尝试过的方法
看了其他人的题解,用其他人的题解在平台上可以通过,但是用自己的IDE跑第五个数据点也是读取数据读到一半卡住,输出错误答案11 163
我想要达到的结果
希望指出我的代码哪里有问题,怎样修改可以通过,最好指出我之所以会出现读取数据读到一半卡住现象的原因