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.

 #include<bits/stdc++.h>
#define INF 1000000
using namespace std;
typedef long long ll;
string s[];
ll n;
int main(){
scanf("%lld",&n);
for(int i=;i<n;i++){
cin>>s[i];
}
int ans=INF;
for(int i=;i<n;i++){
int cnt=;
for(int j=;j<n;j++){
string temp=s[j]+s[j];
if(temp.find(s[i])==-){
cout<<-<<endl;
return ;
}//不需判断是否相等,因为相等时,步数为0
cnt+=temp.find(s[i]);
}
ans=min(ans,cnt);
}
cout<<ans<<endl;
return ;
}

#410(div2)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. Mike and strings 798B

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

  3. CF410div2 B. Mike and strings

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

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

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

  5. Mike and strings

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

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

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

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

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

  8. Codeforces#543 div2 B. Mike and Children(暴力?)

    题目链接:http://codeforces.com/problemset/problem/1121/B 题意 给n个数 最多的对数 其中每一对(i,j)的ai+aj都相等(不知道怎么解释.... 判 ...

  9. codeforces 798B - Mike and strings

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

随机推荐

  1. C#仿QQ设置界面导航

    效果预览,选择左边标签,右边内容会自动滚动到适当位置 public class AnchorPanel { List<PanelMenu> lst = new List<PanelM ...

  2. 为jquery添加扩展标准思路

    jquery扩展分为对象扩展和jquery本身类扩展: 对象扩展: (function($){ $.fn.abc = function(){ console.log($(this).get(0)); ...

  3. 第三篇、javascript整数和字符串

    一.整数 JavaScript中不区分整数值和浮点数值,JavaScript中所有数字均用浮点数值表示. 转换: parseInt(..)    将某值转换成数字,不成功则NaN parseFloat ...

  4. 20145229吴姗珊《JAVA程序设计》第一周学习总结

    教材学习内容总结 第一章 JAVA 平台概论 1.JAVA不仅仅是一门程序设计语言,还是标准规范 2.1995年5月23日被公认为JAVA的诞生日 3.J2SE包含了JDK和JAVA程序语言 4.三大 ...

  5. Linux iptables 从入门到放弃

    iptables表(iptables)和链(chains)    描述完iptables术语后,相信大家对iptables的表和链有了初步的了解.默认情况下,Iptables根据功能和表的定义划分包含 ...

  6. bash rz 上传文件失败问题

    原文链接: https://blog.csdn.net/heavendai/article/details/7549065 单独用rz会有两个问题:上传中断.上传文件变化(md5不同), 解决办法是上 ...

  7. Spring Boot- 用idea新建spring boot web项目

    1.新建project 2.选择Spring Initializr,next 3.输入项目信息,next 4.选择web依赖以及Spring Boot的版本,next 5.Finish 6.Enabl ...

  8. ajax如何处理返回的数据格式是xml的情况

    <!DOCTYPE html> <html> <head> <title>用户注册</title> <meta charset=&qu ...

  9. HTML5学习笔记——1

    1.视频 <vedio src  controls  height width autoplay loop preload > </vedio> <source src ...

  10. 分享知识-快乐自己:大数据(hadoop)环境搭建

    大数据 hadoop 环境搭建: 一):大数据(hadoop)初始化环境搭建 二):大数据(hadoop)环境搭建 三):运行wordcount案例 四):揭秘HDFS 五):揭秘MapReduce ...