简单题
View Code
#include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define maxn 105 #define inf 0x3f3f3f3f struct Elem { char *s; int len; }elem[maxn]; int n; char st[maxn][maxn]; bool cmp(const Elem &a, const Elem &b) { if (a.len == b.len) return strcmp(a.s, b.s) < 0; return a.len < b.len; } void scan(char *a) { scanf("%s", a); } void print(char *a) { printf("%s\n", a); } int mismatch(char *a, char *b, int len) { for (int i = 0; i < len; i++) if (a[i] != b[i]) return i; return -1; } int main() { //freopen("t.txt", "r", stdin); while (scanf("%d", &n), n) { for_each(st, st + n, scan); //for_each(st, st + n, print); for (int i = 0; i < n; i++) { elem[i].s = st[i]; elem[i].len = strlen(st[i]); reverse(elem[i].s, elem[i].s + elem[i].len); } sort(elem, elem + n, cmp); int ans = inf; for (int i = 0; i < n - 1; i++) if (elem[i].len == elem[i + 1].len) { int temp = mismatch(elem[i].s, elem[i + 1].s, elem[i].len); ans = min(ans, elem[i].len - temp - 1); } for (int i = 0; i < n; i++) ans = min(ans, elem[i].len - 1); printf("%d\n", ans); } return 0; }