Created
February 19, 2009 06:31
-
-
Save scvalex/66763 to your computer and use it in GitHub Desktop.
This file contains 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 <string.h> | |
#include <stdlib.h> | |
#define MAXN 5000000 | |
int n; | |
char c[MAXN], *a[MAXN]; | |
int pstrcmp(const void **x, const void **y) { | |
return strcmp(*(char**)x, *(char**)y); | |
} | |
int comlen(char *p, char *q) { | |
int i = 0; | |
while (*p && (*p++ == *q++)) | |
++i; | |
return i; | |
} | |
int main(int argc, char *argv[]) { | |
int i; | |
FILE *fi = fopen("james.txt", "r"); | |
char ch; | |
while (fread(&ch, 1, 1, fi)) { | |
a[n] = &c[n]; | |
c[n++] = ch; | |
} | |
c[n] = 0; | |
fclose(fi); | |
qsort(a, n, sizeof(char*), pstrcmp); | |
int maxlen = 0, maxi = 0; | |
for (i = 0; i < n-1; ++i) | |
if (comlen(a[i], a[i+1]) > maxlen) { | |
maxlen = comlen(a[i], a[i+1]); | |
maxi = i; | |
} | |
printf("%.*s\n", maxlen, a[maxi]); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment