B. Mike and strings
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples
Input
4
xzzwo
zwoxz
zzwox
xzzwo
Output
5
Input
2
molzv
lzvmo
Output
2
Input
3
kc
kc
kc
Output
0
Input
3
aa
aa
ab
Output
-1
Note

In the first sample testcase the optimal scenario is to perform operations in such a way as to transform all strings into "zwoxz".

题意: 你可以将任意字符串第一个字符移到字符串尾部,可以重复此过程,问最少多少步,使得所有字符串相等

 #include <iostream>
#include <cstring>
#include <string>
using namespace std; int get_step(string a, string b){
int len = a.length();
int ans = ;
for(int i = ; i < len; i++){
if(a == b)
return ans;
//string b(b, 1);
string t = "";
t += b[];
b.erase(,);
b += t;
//cout << i << " " << b << endl;
ans++;
}
return -;
} int main(){
int n;
cin >> n;
string str[];
for(int i = ; i <= n; i++){
cin >> str[i];
}
int num = ;
for(int i = ; i <= n; i++){
int cmp = num;
num = ;
for(int j = ; j <= n; j++){
if(get_step(str[i], str[j]) == -){
cout << - << endl;
return ;
}
num += get_step(str[i], str[j]);
}
num = min(num,cmp);
}
cout << num << endl;
return ;
}

Mike and strings 798B的更多相关文章

  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. CF410div2 B. Mike and strings

    /* CF410div2 B. Mike and strings http://codeforces.com/contest/798/problem/B 字符串 暴力 题意:给你n个串,每次操作可以将 ...

  3. Mike and strings CodeForces - 798B (简洁写法)

    题目链接 时间复杂度 O(n*n*|s| ) 纯暴力,通过string.substr()函数来构造每一个字符串平移后的字符串. #include <iostream> #include & ...

  4. Mike and strings CodeForces - 798B (又水又坑)

    题目链接 题意:英语很简单,自己取读吧. 思路: 既然n和i字符串的长度都很小,最大才50,那么就是只要能出答案就任意暴力瞎搞. 本人本着暴力瞎搞的初衷,写了又臭又长的200多行(代码框架占了50行) ...

  5. codeforces 798B - Mike and strings

    感觉自己好咸鱼呀……B题写了这么久,虽然可以算作1A(忽略一次少include一个头文件的CE)…… 思想很简单,每次选定一个字符串作为目标字符串,然后把其他所有字符串都当做测试字符串,计算出总共需要 ...

  6. 【codeforces 798B】Mike and strings

    [题目链接]:http://codeforces.com/contest/798/problem/B [题意] 给你n个字符串; 每次操作,你可以把字符串的每个元素整体左移(最左边那个字符跑到最后面去 ...

  7. #410(div2)B. Mike and strings

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  8. Codeforces Round #410 (Div. 2)B. Mike and strings(暴力)

    传送门 Description Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters. In ...

  9. Mike and strings

    Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters. In one move he can ...

随机推荐

  1. 初识Swift中的值和引用,循坏引用、代理的注意点

    1.0 在Swift中分有值类型和引用类型 Int .String . 结构体和枚举都属于值类型, 将值类型传递给方法是,将在内存中创建其副本,并传递这个副本:这样我们就可以随心所欲修改它,而不用担心 ...

  2. eclipse git 创建新分支 合并分支 删除分支

    创建分支: 合并分支: 删除分支:

  3. putty 链接亚马逊服务器

    使用 PuTTY 从 Windows 连接到亚马逊云的 Linux 实例 转载 2016年07月22日 14:09:47   使用 PuTTY 从 Windows 连接到亚马逊云的 Linux 实例 ...

  4. MATLAB 提取头发(最大连通域)

    I= imread('2.jpg'); figure(), imshow(I) R=I(:,:,); G=I(:,:,); B=I(:,:,); [m,n]=size(r); mask=zeros(m ...

  5. Linux命令_1

    文件和目录命令 从P19开始的笔记 目标 查看目录内容 ls 切换目录 cd 创建和删除操作 touch mkdir rm 拷贝和移动文件 cp mv 查看文件内容 cat more grep 其他 ...

  6. Spring-data-redis redis

    什么是spring-data-redis spring-data-redis是spring-data模块的一部分,专门用来支持在spring管理项目对redis的操作,使用java操作redis最常用 ...

  7. RealtimeRendering III

    [RealtimeRendering III] 1.砖块渲染实例. 1)brick & mortar diffuse texture. 2)brick & mortar gloss t ...

  8. appium+python自动化测试

    appium+python自动化测试 标签(空格分隔): appium 获取APP的包名 1.aapt即Android Asset Packaging Tool,在SDK的build-tools目录下 ...

  9. 【Django】关于ORM的使用

    添加模型并映射到数据库中: 1. 在 settings.py 中,配置好 DATABASES ,做好数据库相关的配置. 以mysql为例: DATABASES = { 'default': { 'EN ...

  10. zabbix 3.2.6+centos 7 +nginx 1.12+ mysql 5.6+ Grafana +php 5.6

    先只记录关键点 1.nginx.MYSQL 用官方的YUM库安装 2.CENTOS升级到最新 3.ZABBIX 官方RPM安装,然后下载源码库,源码中的PHP复制至NGINX的配置目录 4.NGINX ...