Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

为什么几乎一样的两份代码,一个过了一个错了

Posted by 1200017623 at 2013-02-02 12:34:29 on Problem 1936
AC的代码:
#include<iostream>
#include<cstring>
using namespace std;
const int MAX = 100005;
static char str1[MAX] = {0},str2[MAX] = {0};
int main(){
	while(cin>>str1>>str2){
		int nLen1 = strlen(str1),nLen2 = strlen(str2);
		if(nLen1 > nLen2){
			printf("No\n");
			continue;
		}
		if(strcmp(str1,str2) == 0){
			printf("Yes\n");
			continue;
		}
		int nPos1 = 0,nPos2 = 0;
		for(;nPos1 < nLen1 && nPos2 < nLen2;++nPos2){
			if(str2[nPos2] == str1[nPos1])++nPos1;
		}
		if(nPos1 == nLen1)printf("Yes\n");
		else printf("No\n");
	}
	return 0;
}

WA的代码:
#include<iostream>
#include<cstring>
using namespace std;
const int MAX = 100005;
static char str1[MAX] = {0},str2[MAX] = {0};
int main(){
	while(cin>>str1>>str2){
		int nLen1 = strlen(str1),nLen2 = strlen(str2);
		if(nLen1 > nLen2){
			printf("No\n");
			continue;
		}
		if(strcmp(str1,str2) == 0){
			printf("Yes\n");
			continue;
		}
		int i = 0,nPos = 0;
		for(;i < nLen1;++i){
			for(;nPos < nLen2;++nPos){
				if(str2[nPos] == str1[i]){
					++nPos;
					break;
				}
			}
			if(nPos == nLen2)break;
		}
		if(i == nLen1)printf("Yes\n");
		else printf("No\n");
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator