(一)读懂题目
关键词:查找单词
方法:枚举
(二)分析算法+时间复杂度和空间复杂度
算法:枚举
时间复杂度:O(n)
空间复杂度:O(n)
(三)代码实现
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
string word,sen;
int len1,len2;
int ans=0,dir=0;
int i,j;
getline(cin,word);
getline(cin,sen);
len1=word.size();
len2=sen.size();
for(i=0;i<len2;i++)
{
for(j=0;j<len1;j++)
{
if(toupper(sen[i+j])!=toupper(word[j]))
{
break;
}
if(i>0&&sen[i-1]!=' ')
{
break;
}
}
if(j==len1&&(sen[i+j]==' '||i+j==len2))
{
ans++;
if(ans==1)
{
dir=i;
}
}
}
if(ans!=0)
{
cout<<ans<<" "<<dir<<endl;
}
else
{
cout<<"-1"<<endl;
}
return 0;
}
(四)总结反思
这题主要考的就是我们对字符串以及字符数组的应用,以及暴力枚举算法,多多尝试即可