传送门

Description

Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters. In one move he can choose a string si, erase the first character and append it to the end of the string. For example, if he has the string "coolmike", in one move he can transform it into the string "oolmikec".

Now Mike asks himself: what is minimal number of moves that he needs to do in order to make all the strings equal?

Input

The first line contains integer n (1 ≤ n ≤ 50) — the number of strings.

This is followed by n lines which contain a string each. The i-th line corresponding to string si. Lengths of strings are equal. Lengths of each string is positive and don't exceed 50.

Output

Print the minimal number of moves Mike needs in order to make all the strings equal or print  - 1 if there is no solution.

Sample Input

4
xzzwo
zwoxz
zzwox
xzzwo
2
molzv
lzvmo
3
kc
kc
kc
3
kc
kc
kc

Sample Output

5

2

0

-1

思路

题解:

数据范围很小,因此直接暴力求解。

#include<bits/stdc++.h>
using namespace std;
const int INF = (1<<30)-1;
const int maxn = 55;
char str[maxn][maxn];
char tmp[maxn],tmp1[maxn]; int main()
{
//freopen("input.txt","r",stdin);
int n,res = INF;
bool flag = false,success = true;
scanf("%d",&n);
for (int i = 0;i < n;i++)
{
scanf("%s",str[i]);
}
int len = strlen(str[0]);
for (int i = 0;i < n;i++)
{
int cnt = 0;
for (int j = 0;j < n;j++)
{
if (i == j)
{
flag = true;
continue;
}
flag = false;
if (strcmp(str[j],str[i]) == 0)
{
flag = true;
continue;
}
strcpy(tmp1,str[j]);
int tmplen = len; while (--tmplen)
{
strncpy(tmp,tmp1+1,len-1);
tmp[len - 1] = tmp1[0];
cnt++;
strcpy(tmp1,tmp);
if (strcmp(tmp1,str[i]) == 0)
{
flag = true;
break;
}
}
if (!flag)
{
success = false;
break;
}
}
if (!flag)
{
success = false;
break;
}
res = min(res,cnt);
}
if (!success) printf("-1\n");
else printf("%d\n",res);
return 0;
}

  

最新文章

  1. ubuntu引导修复
  2. nodejs初探(一)环境搭建,开发工具安装
  3. hexdump—Linux系统的二进制文件查看工具
  4. UIView 周围出现黑线的解决方法
  5. C++实现python标准库中的Counter
  6. go语言使用redis —— redigo
  7. 【Quick 3.3】资源脚本加密及热更新(一)脚本加密
  8. c++学籍管理系统
  9. IOS开发创建开发证书及发布App应用(一)——流程说明
  10. GO 语言简介(网摘)
  11. python函数调用之自我调用与C++比较
  12. 测试APPEND INSERT是否产生UNDO信息的过程
  13. Docker 学习7 Dockerfile详解
  14. windows安装tf
  15. 使用nexus搭建maven私服教程详解
  16. 20165315 C语言学习情况与Java学习目标
  17. leetcode748
  18. rabbitmq系列一 之简单队列
  19. jQuery学习- 子选择器与可见性选择器
  20. IVF link error错误不显示的问题

热门文章

  1. AtCoder Beginner Contest 133-C - Remainder Minimization 2019
  2. java 接口 以及 与抽象类的区别
  3. 通过设置代理解决AndroidStudio无法下载gradle问题
  4. C# xml格式字符串,插入到数据库出现非法字符
  5. 【汇总目录】eShopOnContainers
  6. java 企业网站源码模版 屏幕自适应 有前后台 springmvc SSM 生成静态化引擎
  7. 详解zabbix中文版安装部署
  8. Python PEP8代码书写规范
  9. 英语单词composing
  10. 使用Windows service创建一个简单的定时器