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

Copy
4
xzzwo
zwoxz
zzwox
xzzwo
output

Copy
5
input

Copy
2
molzv
lzvmo
output

Copy
2
input

Copy
3
kc
kc
kc
output

Copy
0
input

Copy
3
aa
aa
ab
output

Copy
-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 <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
#include <xfunctional>
#define ll long long
#define mod 998244353
using namespace std;
int dir[][] = { {,},{,-},{-,},{,} };
const int maxn = ;
const long long inf = 0x7f7f7f7f7f7f7f7f; int main()
{
int n;
cin >> n;
vector<string> s(n);
for (int i = ; i < n; i++)
cin >> s[i];
int len = s[].size();
int dp=1e9;
for (int i = ; i < n; i++)
{
int cnt = ;
for (int j = ; j < n; j++)
{
string t=s[j];
if (j != i)
{
for (int k = ; k < len; k++)
{
if (k == len - && t != s[i])
{
cout << -;
return ;
}
if (t == s[i])
break;
else
{
char first=t[];
t.erase(,);
t += first;
cnt++;
}
}
}
}
dp = min(dp, cnt);
}
cout << dp;
return ;
}

Mike and strings的更多相关文章

  1. Mike and strings 798B

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

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

  3. CF410div2 B. Mike and strings

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

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

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

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

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

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

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

  9. 【codeforces 798B】Mike and strings

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

随机推荐

  1. Go 与 PHP 的语法对比

    Go 是由 Google 设计的一门静态类型的编译型语言.它有点类似于 C,但是它包含了更多的优点,比如垃圾回收.内存安全.结构类型和并发性.它的并发机制使多核和网络机器能够发挥最大的作用.这是 Go ...

  2. .Net 程序代码混淆加密工具 ILProtector

    我的项目中某一部分信息比较敏感,但是.Net程序反编译之后连注释都看得到.需要把exe保护起来,如:代码混淆之后再加壳. Bing到一款.Net混淆工具  ILProtector   作为资深工具党, ...

  3. HTML5基础-新增标签+新增属性+布局案例

    html5中常用的结构标签 article 文章 header 头部 nav 导航 section 区域 aside 侧边栏 hgroup 区块的相关信息 figure 定义一组内容及标题 figca ...

  4. HTML指定页面编码

    HTML指定页面编码 <meta charset="UTF-8" />

  5. intellji IDEA 2019版激活码(亲测可用 2019年10月14日08:53:54)

    MNQ043JMTU-eyJsaWNlbnNlSWQiOiJNTlEwNDNKTVRVIiwibGljZW5zZWVOYW1lIjoiR1VPIEJJTiIsImFzc2lnbmVlTmFtZSI6I ...

  6. docker搭建环境积累

    weblogic12搭建 sudo docker pull ismaleiva90/weblogic12 sudo docker run -d -p : -p : ismaleiva90/weblog ...

  7. opencv —— copyMakeBorder 扩充图像边界

    扩充图像边界:copyMakeBorder 函数 在图像处理过程中,因为卷积算子有一定大小,所以就会导致图像一定范围的边界不能被处理,这时就需要将边界进行适当扩充. void copyMakeBord ...

  8. Go 使用小记

    1.不能使用在运行时计算的值实例化这样的数组. 而是使用make初始化具有所需长度的切片. db := ConnMysql() rows, err := db.Query("select r ...

  9. 使用 VMware Workstation Pro 安装新的虚拟机

     一.连接服务器 (1)“文件”右键 (2)输入用户名.密码连接服务器 二.创建新的虚拟机 (1) (2) (3) (4) (5) (6) (7) (8)  三.配置服务器 配置文档 - 链接:htt ...

  10. Keras高层API之Metrics

    在tf.keras中,metrics其实就是起到了一个测量表的作用,即测量损失或者模型精度的变化.metrics的使用分为以下四步: step1:Build a meter acc_meter = m ...