RE因为数组不够大、WA因为检索条件不全
查看原帖
RE因为数组不够大、WA因为检索条件不全
871598
StayHungry1206楼主2024/3/4 16:34

我的理解是子串查找问题,下面是两种思路,但第二种更好理解,优先查找最特殊的子串,因为题给条件限制了单一系统,即三种情况。

gets()c++编译不通过,这个函数本身就不安全,c++中不建议使用; cin.get()和cin.getline()都要指定读取长度,不方便; 所以最好用string里的getline(cin,str); 本来以为cin4里的输入数据很大要快速查找什么的,但错的一直都是检索的条件和数组越界: “\r\n”只是判断到“\r\”就认定为“\r\n”,事实上忽略了“\r\r”的情况,导致部分数据输出错误,修正后原来的WA全过; 数组开到10,0000,原来的RE全过; c++版本:


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;
const int N = 100000;
int main()
{
		//	char str[N];
		//	cin.get(str,N);
		string str;
		getline(cin,str);
		//	printf("\n\n\n");
		//	puts(str);
		
		for(int i=0;;i++){
			if(str[i]=='\\'){
				if(str[i+1]=='n'){
					printf("linux");
					break;
				}else if(str[i+1]=='r'){
					if(str[i+2]=='\\'){
						if(str[i+3]=='n'){
							printf("windows");
							break;//TODO
						}
					}else{
						printf("mac");
						break;
					}
				}
			}
//			if(str[i]=='\\'&&str[i+1]=='r'&&str[i+2]=='\\'&&str[i+3]=='n'){
//				cout<<"windows";
//				break;
//			}
//			if(str[i]=='\\'&&str[i+1]=='n'){
//				cout<<"linux";
//				break;
//			}
//			if(str[i]=='\\'&&str[i+1]=='r'){
//				cout<<"mac";
//				break;
//			}
		}//TODO		
	return 0;
}

C语言版本:

#include <stdio.h>

const int N = 100000;
int main()
{
	char str[N];
	gets(str);
	for(int i=0;str[i]!=0;i++){
		if(str[i]=='\\'){
			if(str[i+1]=='n'){
				printf("linux");
				break;
			}else if(str[i+1]=='r'){
				if(str[i+2]=='\\'){
					if(str[i+3]=='n'){
						printf("windows");//子串查找
						break;//TODO
					}
				}else{
					printf("mac");
					break;
				}
			}
		}
	}		
	return 0;
}
2024/3/4 16:34
加载中...