Last active
April 29, 2017 08:59
-
-
Save znxkznxk1030/b9a5ba486642c700e673b6eb18088066 to your computer and use it in GitHub Desktop.
#410_B.cpp
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 <iostream> | |
#include <cstdio> | |
#include <stack> | |
#include <queue> | |
#include <algorithm> | |
#include <cstring> | |
#include <cmath> | |
#define MAX(a,b) (a)>(b)?(a):(b) | |
#define MIN(a,b) (a)>(b)?(b):(a) | |
#define ll long long int | |
#define ull unsigned long long int | |
#define INF 10000000 | |
int n, l, m; | |
char s[51][51]; | |
int num[51]; | |
void input() { | |
scanf("%d", &n); | |
for (int i = 0; i < n; i++) | |
{ | |
scanf("%s", s[i]); | |
} | |
l = strlen(s[0]); | |
char tmp[51]; | |
strcpy(tmp, s[0]); | |
m = l; | |
for (int i = 1; i < l; i++) | |
{ | |
char t = tmp[0]; | |
for (int j = 1; j < l; j++) | |
{ | |
tmp[j - 1] = tmp[j]; | |
} | |
tmp[l - 1] = t; | |
if (strcmp(tmp, s[0]) == 0) | |
{ | |
m = i; | |
break; | |
} | |
} | |
} | |
int numbering() { | |
memset(num, -1, sizeof(num)); | |
num[0] = 0; | |
for (int i = 1; i < n; i++) | |
{ | |
for (int j = 0; j < l; j++) | |
{ | |
bool f = true; | |
for (int k = 0; k < l; k++) | |
{ | |
int cur = (j + k) % l; | |
if (s[0][k] != s[i][cur]) | |
{ | |
f = false; | |
break; | |
} | |
} | |
if (f) | |
{ | |
num[i] = j; | |
break; | |
} | |
} | |
if (num[i] == -1) | |
return -1; | |
} | |
return 0; | |
} | |
int main() { | |
input(); | |
if (numbering() == -1) | |
printf("-1\n"); | |
else { | |
int ans = INF; | |
for (int i = 0; i < l; i++) | |
{ | |
int sum = 0; | |
for (int j = 0; j < n; j++) | |
{ | |
int tmp = num[j] - i; | |
if (tmp < 0) tmp += l; | |
sum += tmp % m; | |
} | |
ans = MIN(ans, sum); | |
} | |
if(ans != INF) | |
printf("%d\n", ans); | |
else printf("-1\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment