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
给你n个字符串,让你找怎么处理使你移动的个数最小,如果移动后还不相等就输出-1
#include <bits/stdc++.h>
using namespace std;
int main(){
int n=;
cin>>n;
string s[];
for(int i=;i<n;i++)
cin>>s[i];
int mi=<<;
int l=s[].length();
for(int i=;i<n;i++){
int t=;
for(int j=;j<n;j++){
if(j!=i){
int k;
for(k=;s[j][k];k++)
if(s[j].substr(k)+s[j].substr(,k)==s[i]){
t+=k;
break;
}
if(k>=l){cout<<"-1"<<endl;return ;}}}
mi=min(t,mi);}
cout<<mi<<endl;
return ;
}

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

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

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

  2. 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 ...

  3. 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 ...

  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) C. Mike and gcd problem

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

  6. Codeforces Round #410 (Div. 2)

    Codeforces Round #410 (Div. 2) A B略..A没判本来就是回文WA了一次gg C.Mike and gcd problem 题意:一个序列每次可以把\(a_i, a_{i ...

  7. set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet

    题目传送门 /* 题意:对于长度为x的子序列,每个序列存放为最小值,输出长度为x的子序列的最大值 set+线段树:线段树每个结点存放长度为rt的最大值,更新:先升序排序,逐个添加到set中 查找左右相 ...

  8. 数论/暴力 Codeforces Round #305 (Div. 2) C. Mike and Frog

    题目传送门 /* 数论/暴力:找出第一次到a1,a2的次数,再找到完整周期p1,p2,然后以2*m为范围 t1,t2为各自起点开始“赛跑”,谁落后谁加一个周期,等到t1 == t2结束 详细解释:ht ...

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

    题目传送门 /* 暴力:每次更新该行的num[],然后暴力找出最优解就可以了:) */ #include <cstdio> #include <cstring> #includ ...

随机推荐

  1. bt5r3开启远程登录

    sshd-generate /etc/init.d/ssh restart

  2. 卓越管理的实践技巧(1)如何进行有效的指导 Guidelines for Effective Coaching

    Guidelines for Effective Coaching 前文卓越管理的秘密(Behind Closed Doors)最后一部分提到了总结的13条卓越管理的实践技巧并列出了所有实践技巧名称的 ...

  3. fork新建进程

    #include <sys/types.h>#include<sys/wait.h>#include<unistd.h>#include<stdio.h> ...

  4. 关于tomcat的classloader的一点想法

    关于tomcat的classloader相关的帖子网上非常多,我觉得比较好的有: https://www.jianshu.com/p/d90e4430b0b9 https://blog.csdn.ne ...

  5. iBatis自动生成工具Abator

    https://blog.csdn.net/k_scott/article/details/8281837 ###首先创建数据库表,然后根据数据库表,生成相应的实体.及其配置文件 https://ww ...

  6. cocos2dx for lua 摄像机移动

    在cocos2dx中,我们想通过移动摄像机来做一些特殊处理,比如将摄像机聚焦在某个物体上,或者摄像机颤抖,摄像机原理观察sprite回收状况等等, 都需要通过相机移动来使用. cocos2dx中的摄像 ...

  7. C语言单链表的实现

    // //  main.c //  gfhjhgdf // //  Created by chenhao on 13-12-23. //  Copyright (c) 2013年 chenhao. A ...

  8. Your Ride Is Here

    纯粹的水题= = /* ID:yk652321 LANG:C++ TASK:ride */ #include<iostream> #include<cstring> #incl ...

  9. redis学习笔记(1)

    最近在学习redis,做了比较详细的学习笔记,分享给大家,欢迎一起讨论和学习 第一部分,简单介绍redis 和 redis的基本操作 NoSQL的特点 : 数据库种类繁多,但是一个共同的特点都是去掉关 ...

  10. Golang 简单 http 代理转发

    程序基本实现了对http的完整转发,目前暂不支持https windows需要在设置中的网络>代理设置为手动,并开启代理服务器,填写ip和端口 // httpForward package ma ...