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. rsync sersync搭建

    目标服务器 192.168.100.47 源服务器    192.168.100.46 目标服务器配置 [root@node01 nodejs]# cat /etc/rsyncd.conf #日志文件 ...

  2. ADO.Net 数据库 删除

    删除数据库里的信息和之前增加,修改大同小异,其写法更加简单,也是把SQL语句写为删除语句 删除一条数据,只需要获取并接收到这条数据唯一的能够代表这条数据的信息,比如主键 代码演示: using Sys ...

  3. JAVA语言 第三周

    第三周,跟随菜鸟教程学习了一些简单的JAVA语法,包括数据类型.变量.修饰符.运算符.循环.数组.继承. 这些在结构中与c++相似,但语法有些不同.在这些方面,我做了一些总结. 开学测试那张卷子,被我 ...

  4. JS-事件对象(鼠标键盘事件)

    一  事件对象 1 需要获取键盘和鼠标的信息的时候,用到事件对象.(e) 例如:document.onclick = function(e){ var  e = e || event;(做兼容) } ...

  5. http协议解析过程

    HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统. 基于HTTP协议的客户端/服务器请求响应机制的信息交换过程包含下面几个步骤: 1)     建立连接:客 ...

  6. el-js中循环遍历el的集合

    遇到问题的代码: var score=0; for(var i=0;i<${fn:length(tMovie.tComments) };i++){ score=${tMovie.tComment ...

  7. 04_web基础(五)之cookie与session

    29.Http协议无记忆带来的问题 什么是会话:可简单理解为:用户开一个浏览器,访问某一个web站点,在这个站点点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一次会话. 在一 ...

  8. 04_web基础(四)之servlet详解

    16.17.18.servlet生命周期 javax.servlet.Servlet接口方法:public String getServletInfo():获取Servlet相关信息(作者,版权,版本 ...

  9. 11.14java课堂测试

    源代码: import java.*; import java.util.*; import java.io.File; import java.io.FileInputStream; import ...

  10. sql注入(一)

    SELECT * FROM users WHERE user='uname' AND password='pass' SELECT * FROM users WHERE user='name' AND ...