传送门

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;
}

  

Codeforces Round #410 (Div. 2)B. Mike and strings(暴力)的更多相关文章

  1. Codeforces Round #410 (Div. 2) B. Mike and strings

    B. Mike and strings time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. Codeforces Round #410 (Div. 2)A B C D 暴力 暴力 思路 姿势/随机

    A. Mike and palindrome time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  3. Codeforces Round #410 (Div. 2)C. Mike and gcd problem

    题目连接:http://codeforces.com/contest/798/problem/C C. Mike and gcd problem time limit per test 2 secon ...

  4. Codeforces Round #410 (Div. 2) A. Mike and palindrome

    A. Mike and palindrome time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  5. Codeforces Round #410 (Div. 2) A. Mike and palindrome【判断能否只修改一个字符使其变成回文串】

    A. Mike and palindrome time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  6. 【推导】Codeforces Round #410 (Div. 2) C. Mike and gcd problem

    如果一开始就满足题意,不用变换. 否则,如果对一对ai,ai+1用此变换,设新的gcd为d,则有(ai - ai+1)mod d = 0,(ai + ai+1)mod d = 0 变化一下就是2 ai ...

  7. Codeforces Round #305 (Div. 2) B. Mike and Fun 暴力

     B. Mike and Fun Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/548/pro ...

  8. Codeforces Round #305 (Div. 2) A. Mike and Fax 暴力回文串

     A. Mike and Fax Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/548/pro ...

  9. Codeforces Round #305 (Div. 1) A. Mike and Frog 暴力

     A. Mike and Frog Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/547/pr ...

随机推荐

  1. P3191 [HNOI2007]紧急疏散EVACUATE

    传送门 这一题很容易想到网络流 一开始傻逼地模拟整个图每一个时间的情况,显然会爆炸 发现我们只要考虑起点到门之间的距离,不用每一步只走一格 所以直接 $BFS$ 预处理距离然后二分答案,网络流判断即可 ...

  2. Ubuntu中找不到pip3命令的解决方法

    Ubuntu中找不到pip3命令的解决方法 Ubuntu 有 python2 和 python3. 今天使用 Ubuntu 中的 python3 时,想要安装第三方库却发现 pip 指向的是 pyth ...

  3. Shell脚本的fork炸弹

    #!bin/bash#功能:快速消耗计算机资源,致使计算机死机#作者:liusingbon#定义函数名为.(点), 函数中递归调用自己并放入后台执行function . { .|.& };.

  4. 原生jdbc操作

    1:加入dbcp连接池依赖 <dependency> <groupId>org.apache.commons</groupId> <artifactId> ...

  5. centos6 安装mysql5.77(开发版)

    1. 配置yum源: [root@yyf ~]#rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm [ ...

  6. thinkphp获取目录的方法

    1.获取根目录 http://localhost/ 下面两种方法效果一样 $_SERVER['REQUEST_SCHEME']."://".$_SERVER['HTTP_HOST' ...

  7. 一次Oday提权批量拿取商城服务器root权限

    此问题影响范围巨大,涉及到所有通过niushop开发的商城,希望通过这篇文章能够引起大家的重视.(注:演示的所用商城已经修复了此漏洞) 严重性:特级 解决方案:1.在用户访问漏洞页时对其身份进行判断: ...

  8. jmeter性能工具 使用手册(一)

    前置条件: 在jmeter官网下载jmter 安装包 电脑有java 环境 使用步骤: 打开jmeter 2.新建线程 Test plan--->add-->theads(users)-- ...

  9. JS原型和原型链(3)

    构造函数创建对象: function Person() { } var person = new Person(); person.name = 'Kevin'; console.log(person ...

  10. python——解释型语言

    编程语言分三大类   :  低级语言  .  汇编语言 .   高级语言. 现代计算机都是基于  图灵机模型 制造的.  因此计算机的内部只能接受二进制数据.而用二进制代码  0  1  描述的指令叫 ...