求助P1308 [NOIP2011 普及组] 统计单词数,玄关
  • 板块灌水区
  • 楼主Like_Amao
  • 当前回复3
  • 已保存回复3
  • 发布时间2024/10/17 19:23
  • 上次更新2024/10/17 20:55:29
查看原帖
求助P1308 [NOIP2011 普及组] 统计单词数,玄关
993044
Like_Amao楼主2024/10/17 19:23

rt,代码如下

#include<bits/stdc++.h>
using namespace std;
int main()
{
	string word,sentence;
	getline(cin,word);
	getline(cin,sentence);
	int len_word=word.size();
	int len_sentence=sentence.size();
	int count_word=-1,start=0,count_same;
	bool is_same=false,have_start=false;
	for(int i=0;i<=len_word-1;i++)
	{
		if(word[i]>='A' and word[i]<='Z')
		{
			word[i]=char(word[i]+32);
		}
	}
	for(int i=0;i<=len_sentence-1;i++)
	{
		if(sentence[i]>='A' and sentence[i]<='Z')
		{
			sentence[i]=char(sentence[i]+32);
		}
	}
	for(int i=0;i<=len_sentence-1;i++)
	{
		count_word++;
		if(word[count_word]==sentence[i])
		{
			is_same=true;
		}
		else
		{
			is_same=false;
			count_word=-1;
		}
		if(count_word==len_word-1)
		{
			count_word=-1;
			if(is_same==true)
			{
				if(have_start==false)
				{
					start=i-len_word+1;
					have_start=true;
				}
				count_same++;
			}
			is_same=false;
		}
	}
	if(count_same==0)
	{
		cout<<"-1";
		return 0;
	}
	cout<<count_same<<" "<<start;
	return 0;
}
2024/10/17 19:23
加载中...