#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string word,passage,check; //word为第一行单词,passage为文章,check用作读入文章中每一个单词
getline(cin,word);
getline(cin,passage);
int cnt=0,ans=0,first=1000000; //first为首次出现的首字母位置,先放到最大为10^6,后期再用min
//ans为出现次数,cnt用于判断单词是否相同,见38行
int wordlen=word.size(),passlen=passage.size(); //长度定义
for( int i=0 ; i<passlen ; i++ ){ //逐字母分析文章
int j=0; //j为check的下标
while( passage[i]!=' '&&i!=passlen ){ //passage[i]不是空格时且非最后一个单词时循环
check[j]=passage[i];
j++;
i++;
}
if( wordlen==j ){ //先判断check单词是否与目标单词等长
for( int k=0 ; k<wordlen ; k++ ){
/*//////////////////////////////////////////////////////////////////////////////////////*/
/* 逐字母判断check与word是否相同(包括大小写转换) */
/*//////////////////////////////////////////////////////////////////////////////////////*/
if( (word[k]>='a'&&check[k]>='a')||(word[k]<='Z'&&check[k]<='Z') )
if( word[k]==check[k] )
cnt++;
if( word[k]<='Z'&&check[k]>='a' )
if( word[k]+'a'-'A'==check[k] )
cnt++;
if( word[k]>='a'&&check[k]<='Z' )
if( word[k]==check[k]+'a'-'A' )
cnt++;
}
if( cnt==wordlen ){ //cnt为word与check相等的字母数,若cnt==wordlen则完全相等
ans++;
first=min( first , i-j ); //判断是否为首次出现,i-j为此次出现的首字母位置
}
}
cnt=0; //cnt归零
}
if( ans!=0 )
cout<<ans<<" "<<first;
else
cout<<-1;
return 0;
}
如上,解题思路很奇怪,尽量标了注释,想听听大佬们的意见,还请大佬们说的通俗易懂些。