思路
- 字符串匹配,暴力匹配就完事了
- 注意匹配和不匹配的数字
int cmp (const void *a,const void *b){
char *c = *(char**)a;
char *d = *(char**)b;
if(strlen(c) != strlen(d))
return strlen(d)-strlen(c);
return strcmp(c,d);
}
char * findLongestWord(char * s, char ** dictionary, int dictionarySize){
int i=0;
int j=0;
int k=0;
int count =0 ;
int len = strlen(s);
//printf("%d\n",dictionarySize);
char **re = (char **)malloc(sizeof(char*)*dictionarySize);
for(i = 0; i < dictionarySize; i++) {
char *res = (char*)malloc(sizeof(char)*1001);
re[i] = res;
}
for(i = 0; i < dictionarySize; i++){
k = 0;
int len1 = strlen(dictionary[i]);
for(j = 0; j < len1; ) {
while(k < len) {
if(dictionary[i][j]==s[k]) {
k++;
j++;
} else {
k++;
}
if(j==strlen(dictionary[i])) {
strcpy(re[count],dictionary[i]);
count++;
break;
}
}
if(k==len)//到头也要break
break;
}
}
if(count==0)
return "";
qsort(re,count,sizeof(re[0]),cmp);
return re[0];
}