CF410div2 B. Mike and strings
/*
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的更多相关文章
- Mike and strings 798B
B. Mike and strings time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 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 ...
- CF410div2 A. Mike and palindrome
/* CF410div2 A. Mike and palindrome http://codeforces.com/contest/798/problem/A 水题 */ #include <c ...
- CF410div2 D. Mike and distribution
/* CF410div2 D. Mike and distribution http://codeforces.com/contest/798/problem/D 构造 题意:给出两个数列a,b,求选 ...
- #410(div2)B. Mike and strings
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- Codeforces Round #410 (Div. 2)B. Mike and strings(暴力)
传送门 Description Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters. In ...
- Mike and strings
Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters. In one move he can ...
- Mike and strings CodeForces - 798B (简洁写法)
题目链接 时间复杂度 O(n*n*|s| ) 纯暴力,通过string.substr()函数来构造每一个字符串平移后的字符串. #include <iostream> #include & ...
- Mike and strings CodeForces - 798B (又水又坑)
题目链接 题意:英语很简单,自己取读吧. 思路: 既然n和i字符串的长度都很小,最大才50,那么就是只要能出答案就任意暴力瞎搞. 本人本着暴力瞎搞的初衷,写了又臭又长的200多行(代码框架占了50行) ...
随机推荐
- 要自己当技术使用astgo运营网络电话系统,必须掌握的基本技术
知道什么是centos 知道怎么远程访问centos服务器 (常用工具 Secure Shell Client.WINSCP) 知道重启服务器的命令是 reboot 知道你的服务器是没有图形界面的,所 ...
- poj1611 并查集 (路径压缩)
http://poj.org/problem?id=1611 题目大意: 有一个学校,有N个学生,编号为0-N-1,现在0号学生感染了非典,凡是和0在一个社团的人就会感染,并且这些人如果还参加了别的社 ...
- codevs1557 热浪(堆优化dijkstra)
1557 热浪 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 德克萨斯纯朴的民眾们这个夏 ...
- POJ3687Labeling Balls
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14278 Accepted: 4162 D ...
- 【BZOJ4566_洛谷3181】[HAOI2016]找相同字符(SAM)
自己yy的方法yyyyyyyy着就A了,写篇博客庆祝一下. 题目: 洛谷3181 分析: SAM(可能是)模板题(不会SAM的同学戳我:[知识总结]后缀自动机的构建). 对\(s1\)建出SAM,用\ ...
- c++ 四种类型转换机制
类型转换机制可以分为:隐式类型转换 和 显示类型转换(强制类型转换) C中的类型转换: 事情要从头说起,这个头就是C语言.我们已经习惯了使用C-like类型转换,因为它强大而且简单. 主要有一下两种形 ...
- Unity相机平滑跟随
简介 unity中经常会用到固定视角的相机跟随,然后百度发现大家都是自己写的,然后偶也写咯一个,分享一下 PS: 由于刚学C#不久,才发现delegate这个东东,也不知道对性能影响大不大,但是看MS ...
- jdk11安装没有jre文件夹
原因:jdk11安装之后是没有jre的 如果需要jre,需要到jdk目录下面去 打开命令窗口,然后执行如下命令: bin\jlink.exe --module-path jmods --a ...
- python gdal 修改shp文件的属性值
driver = ogr.GetDriverByName('ESRI Shapefile')datasource = driver.Open(shpFileName, 1)layer = dataso ...
- 15个最受欢迎的Python开源框架(转)
原文地址:http://blog.jobbole.com/72306/ Django: Python Web应用开发框架 Django 应该是最出名的Python框架,GAE甚至Erlang都有框架受 ...