Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
为什么错了……求教/* 我的想法是:先给字符串排序,这样保证有共同前缀的字符串都在一起 之后依次判断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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator