40分求助~~注释已写!

P1308 [NOIP2011 普及组] 统计单词数

hack: ```text abc abcde ```
by Ihave4oranges @ 2024-04-25 21:13:33


```cpp #include<bits/stdc++.h> using namespace std; string s;//文章 string x; int num,cnt; queue <int> q; int main() { getline(cin,x); int len=x.length();//单词长度 getline(cin,s); x[0]=tolower(x[0]);//目标字符串变小写 for(int i=0;i<s.length();i++) s[i]=tolower(s[i]);//文章变小写 for(int i=0;i<s.length();i++) { if(s[i]==x[0]&&(i==0||s[i-1]==' '))//找到了首字母 //修改这一行 { if(i+len<=s.size()&&s[i+len]==' '&&s.substr(i,len)==x)//找到了单词 //还有这一行 { //cout<<s.substr(i,len)<<endl; cnt++; q.push(num);//推入出现位置 } } num++; } if(!cnt) cout<<-1; else cout<<cnt<<' '; if(!q.empty()) cout<<q.front();//输出首次出现位置 return 0; } ```
by Ihave4oranges @ 2024-04-25 21:16:52


我才发现你单词前的空格也没有判
by Ihave4oranges @ 2024-04-25 21:17:39


还要改一个地方,看代码
by Ihave4oranges @ 2024-04-25 21:17:55


@[Ihave4oranges](/user/310142) 超级感谢dalao!orz!
by 77Glorious @ 2024-04-25 21:20:47


|