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-05-15 11:05:30 on Problem 2334
/*
我的想法是:先给字符串排序,这样保证有共同前缀的字符串都在一起
之后依次判断0,1  1,2  ...  n-2,n-1的缀长度并直接统计
字符串相同和有空格的情况我都考虑了……还是WA
*/


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

static char *str[10002];

bool cmp(char* str1,char* str2){
	return strcmp(str1,str2) < 0;
}

int PerfixLen(const char* str1,const char* str2){
	int len = 0;
	for(;*str1 && *str1 == *str2;++str1,++str2)++len;
	return len;
}

int main(){
	int N,TotalLen = 0;
	char tmpstr[256] = {0};
	scanf("%d",&N);
	getchar();
	for(int i = 0;i < N;++i){
		gets(tmpstr);
		str[i] = new char[strlen(tmpstr) + 1];
		strcpy(str[i],tmpstr);
		TotalLen += strlen(str[i]);
	}

	sort(str,str+N,cmp);

	for(int i = 0;i < N - 1;++i){
		TotalLen -= (PerfixLen(str[i],str[i+1]) - 1);
	}
	printf("%d\n",TotalLen);

	for(int i = 0;i < N;++i)delete str[i];
	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