传送门

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. Python中包的定义

    简单来说,包就是文件夹,但该文件夹下必须存在 __init__.py 文件, 该文件的内容可以为空.__init__.py 用于标识当前文件夹是一个包. 实例子 test.pypackage_dc36 ...

  2. R语言——ifelse函数

    在数据处理中,经常会遇到要对判断结果做处理的情况.if函数是经常遇到的. ifelse(cond,statment1,statment2) 如果cond成立,执行statment1,否则执行statm ...

  3. 关于webpack高版本向低版本切换 如何切换?

    卸载:npm uninstall webpack -g 重新安装:npm install webpack@3.7.1 -g 直接安装指定版本就行了,如安装 2.4.1 版:cnpm install w ...

  4. Qt Creator 不能输入中文怎么解决?

    Qt Creator 2.7.2不能输入中文怎么解决?之前提的问题自己后来找到解决方法后就忘了, 方法很简单,只要设置一下环境变量就行了export QT_IM_MODULE=ibus qt5.4.r ...

  5. linux--基础知识5

    #文件合并与文件归档 #cat /etc/passwd > new_pass.txt (创建一个新的文档并将cat/etc/passwd的内容合并进来) #echo "xxxx&quo ...

  6. [Tyvj1423]GF和猫咪的玩具(最短路)

    [Tyvj1423]GF和猫咪的玩具 题目描述 GF同学和猫咪得到了一个特别的玩具,这个玩具由n个金属环(编号为1---n),和m条绳索组成,每条绳索连接两个不同的金属环,并且长度相同.GF左手拿起金 ...

  7. sqlmap 基本使用步骤(二)

    post------------------------------------------------------------------1.使用 -rpython sqlmap.py -r pos ...

  8. 02.action--新增精灵知识点

    import cocos from cocos.actions import * class HelloWorld(cocos.layer.ColorLayer): # ColorLayer子类化为具 ...

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

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

  10. NIO编程模式示例

    1. 服务端 import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.*; im ...