-
-
Save bojieli/b739dd8c1fa7f85982da to your computer and use it in GitHub Desktop.
最短前缀
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
int main() { | |
int num = 0; | |
int i, j, k; | |
char a[1001][22] = {0}; | |
int idx[1001], tmpidx; | |
char tmp[22] = {0}; | |
while (scanf("%s", a[num++]) != EOF); | |
for (i = 0; i < num; i++) | |
idx[i] = i; | |
for (i = 0; i < num; i++) | |
for (j = i+1; j < num; j++) | |
if (strcmp(a[i], a[j]) > 0) { | |
memcpy(tmp, a[i], sizeof(tmp)); | |
memcpy(a[i], a[j], sizeof(tmp)); | |
memcpy(a[j], tmp, sizeof(tmp)); | |
tmpidx = idx[i]; | |
idx[i] = idx[j]; | |
idx[j] = tmpidx; | |
} | |
for (k = 0; k < num; k++) { | |
int l, maxj; | |
for (i = 0; i < num; i++) | |
if (idx[i] == k) | |
break; | |
l = strlen(a[i]); | |
if (i > 0) { | |
for (j = 0; j < l && a[i][j] == a[i-1][j]; j++); | |
maxj = j; | |
} | |
if (i < num-1) { | |
for (j = 0; j < l && a[i][j] == a[i+1][j]; j++); | |
maxj = (j > maxj ? j : maxj); | |
} | |
strcpy(tmp, a[i]); | |
tmp[maxj+1] = '\0'; | |
printf("%s %s\n", a[i], tmp); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment