想问一下各位大佬为什么会超时,测试的时候感觉没什么问题。
查看原帖
想问一下各位大佬为什么会超时,测试的时候感觉没什么问题。
433290
jthilove1314楼主2022/12/5 00:40
#include<iostream>
#include<string> 
using namespace std;
bool equal(string str1,string str2)
{
	if(str1.length()!=str2.length())
	return false;
	for(int i=0;i<str1.length();i++)
	{
		if(str1[i]==str2[i])
		{
			continue;
		}
		if(str1[i]>='A'&&str1[i]<='Z')
		{
			if(str1[i]!=str2[i]-32)
			return false;
		}
		else if(str1[i]>='a'&&str1[i]<='z')
		{
			if(str1[i]!=str2[i]+32)
			return false;
		}
		else
		return false;
	}
	return true;
}
int main()
{
	string str;
	int word_num=0;
	int number=0;
	int index=-1;
	cin>>str;
	string word;
	char c='0';
	while(c!='\n')
	{
		cin>>word;
		if(equal(str,word))
		{
			word_num++;
			if(index==-1)
			index=number;
		}
		number++;
		cin.get(c);
	} 
	if(index!=-1)
	{
		cout<<word_num<<" "<<index<<endl;
	}
	else
	cout<<index<<endl; 
	return 0; 
}
2022/12/5 00:40
加载中...