/*
CF410div2 B. Mike and strings
http://codeforces.com/contest/798/problem/B
字符串 暴力
题意:给你n个串,每次操作可以将某个串的第一个字符放到最后去,
问最少的次数,使得所有字符串都相同
思路:先将所有字符串复制成二倍,然后暴力枚举要变成的串
注意数组都要开二倍。。
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <iostream>
#include <map>
#include <set>
//#define test
using namespace std;
const int Nmax=;//空间记得要开两倍
char s[Nmax][Nmax];
char tmp[Nmax];
int ans[Nmax][Nmax];
int n,len=;
int check(int a,int b)
{
for(int i=b;i<=b+len-;i++)
{
if(tmp[i-b+]!=s[a][i])
return ;
}
return ;
}
int main()
{
#ifdef test
#endif
//freopen("2.in","r",stdin);
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%s",s[i]+);
//printf("%s\n",s[i]+1);
len=max(len,(int)strlen(s[i]+));
for(int j=len+;j<=len<<;j++)
s[i][j]=s[i][j-len];
}
for(int i=;i<=len;i++)//枚举匹配头
{
int k=i;
for(int j=;j<=len;j++)
tmp[j]=s[][k++];
for(int j=;j<=n;j++)
{
int flag=;
for(int t=;t<=len;t++)
{
if(check(j,t))
{
ans[j][i]=t-;
//printf("ans[%d][%d]=%d\n",j,i,ans[j][i]);
flag=;
break;
}
}
if(flag)
{
printf("-1\n");
return ;
}
}
}
for(int i=;i<=len;i++)
ans[][i]=i-;
int res=1e9,book=;
for(int t=;t<=len;t++)
{
book=;
for(int i=;i<=n;i++)
book+=ans[i][t];
//printf("book[%d]:%d\n",t,book);
res=min(res,book);
}
printf("%d\n",res);
return ;
}

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

    /* CF410div2 A. Mike and palindrome http://codeforces.com/contest/798/problem/A 水题 */ #include <c ...

  4. CF410div2 D. Mike and distribution

    /* CF410div2 D. Mike and distribution http://codeforces.com/contest/798/problem/D 构造 题意:给出两个数列a,b,求选 ...

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

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

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

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

  7. Mike and strings

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

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

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

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

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

随机推荐

  1. 【BeijingWc 2008】 秦腾与教学评估

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1271 [算法] 二分 [代码] #include<bits/stdc++.h& ...

  2. SPOJ - QMAX3VN (4350) splay

    SPOJ - QMAX3VN 一个动态的序列 ,在线询问某个区间的最大值.关于静态序列的区间最值问题,用ST表解决,参考POJ 3264 乍一看上去 splay可以轻松解决.书上说可以用块状链表解决, ...

  3. bag of words in c++

    #include <iostream> #include <vector> #include <cstddef> #include <string> # ...

  4. Systick 更新

    之前写的systick_config(loadvalue) 根据系统时钟为72Mhz来写的,如果system clock不是72MHz怎么办? 重新写了一下,先获取,系统时钟频率. //参数为ms v ...

  5. 使用 Jenkins + GitHub + Nginx + HTTPS 搭建静态网站

    参考https://www.imooc.com/article/20079 http://www.haoduoyu.cc/

  6. Ansi2Utf8 小工具

    将GB2312编码的文件转成Unity使用的UTF8无bom格式 主要用批处理执行 Ansi2Utf8.exe XXXXX.txt  源代码 using System; using System.Co ...

  7. [Swift通天遁地]九、拔剑吧-(7)创建旋转和弹性的页面切换效果

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  8. blockhouses

    题意 : 给你一张图上面" X " 代表墙 , " . " 代表空地 , 让你在空地上放置炮台 , 条件是 不能 让彼此的炮台 可以互相看见 ( 隔着墙就看不见 ...

  9. Linq学习(五)-多表连接

    本将主要介绍 内连接与 外连接 1.join Linq to sql from a in Blog_Users join b in Blog_UserInfo on a.UserId equals b ...

  10. <form> 标签

    <form method="传送方式" action="服务器文件"> action :浏览者输入的数据被传送到的地方,比如一个PHP页面(save ...