萌新头痛┭┮﹏┭┮为什么加了个while循环会变得超时!
查看原帖
萌新头痛┭┮﹏┭┮为什么加了个while循环会变得超时!
404783
w158797楼主2023/1/10 09:31
#include<iostream>
using namespace std;
int main(){
	int position=-1,first,count=0;
	string require,give;
	cin>>require;
	int size=require.size();
	char now;
	cin.get();
	while(cin.get(now)){
		position++;
		if(now=='\n') break;
		else if(now==' ') {
		}
		else{
			int start=position;
			int there=0;
			bool result=true;			while((now!=''||now!='\n')&&there<size){			
if(now!=require[there]&&now!=require[there]+32&&now!=require[there]-32){
            
	result=false;
	break;
				}
		there++;
		position++;
		cin.get(now);
	}
if(result){
if(there==size&& (now==''||now=='\n')){
		count++;
		if(count==1){
		first=start;			
			}
		}
	}
	/*while(now!=' '){
if(now=='\n') break;
	position++;
	cin.get(now);
		}*/就是这里(if我这个字符不匹配我要讲这个字符读完)
			if(now=='\n') break;
		}                               
	}
	if(count==0) cout<<-1;
	else cout<<count<<" "<<first;
	return 0;
} 

###关键就是这个while循环,这段代码的意思是if我这个字符串不匹配了,我就要读取到空格或者回车为止,把这个字符串给跳过,但是我发现if加了这个就超时!!!而且是数据量多不超时,数据量少就超时!!无语了....结果和逻辑都是对的

2023/1/10 09:31
加载中...