Mike and strings 798B
2 seconds
256 megabytes
standard input
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?
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.
Print the minimal number of moves Mike needs in order to make all the strings equal or print - 1 if there is no solution.
4
xzzwo
zwoxz
zzwox
xzzwo
5
2
molzv
lzvmo
2
3
kc
kc
kc
0
3
aa
aa
ab
-1
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 <cstring>
#include <string>
using namespace std; int get_step(string a, string b){
int len = a.length();
int ans = ;
for(int i = ; i < len; i++){
if(a == b)
return ans;
//string b(b, 1);
string t = "";
t += b[];
b.erase(,);
b += t;
//cout << i << " " << b << endl;
ans++;
}
return -;
} int main(){
int n;
cin >> n;
string str[];
for(int i = ; i <= n; i++){
cin >> str[i];
}
int num = ;
for(int i = ; i <= n; i++){
int cmp = num;
num = ;
for(int j = ; j <= n; j++){
if(get_step(str[i], str[j]) == -){
cout << - << endl;
return ;
}
num += get_step(str[i], str[j]);
}
num = min(num,cmp);
}
cout << num << endl;
return ;
}
Mike and strings 798B的更多相关文章
- 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 B. Mike and strings
/* CF410div2 B. Mike and strings http://codeforces.com/contest/798/problem/B 字符串 暴力 题意:给你n个串,每次操作可以将 ...
- Mike and strings CodeForces - 798B (简洁写法)
题目链接 时间复杂度 O(n*n*|s| ) 纯暴力,通过string.substr()函数来构造每一个字符串平移后的字符串. #include <iostream> #include & ...
- Mike and strings CodeForces - 798B (又水又坑)
题目链接 题意:英语很简单,自己取读吧. 思路: 既然n和i字符串的长度都很小,最大才50,那么就是只要能出答案就任意暴力瞎搞. 本人本着暴力瞎搞的初衷,写了又臭又长的200多行(代码框架占了50行) ...
- codeforces 798B - Mike and strings
感觉自己好咸鱼呀……B题写了这么久,虽然可以算作1A(忽略一次少include一个头文件的CE)…… 思想很简单,每次选定一个字符串作为目标字符串,然后把其他所有字符串都当做测试字符串,计算出总共需要 ...
- 【codeforces 798B】Mike and strings
[题目链接]:http://codeforces.com/contest/798/problem/B [题意] 给你n个字符串; 每次操作,你可以把字符串的每个元素整体左移(最左边那个字符跑到最后面去 ...
- #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 ...
随机推荐
- eclipse git 删除内容
- shiro 权限 URL 配置细节
- tensorflow 高级api使用分布式之配置
"""Constructor. Sets the properties `cluster_spec`, `is_chief`, `master` (if `None` i ...
- 文件上传以及JS链式结构
文件上传: 文件上传使用FileUpload控件,使用控件的SaveAs方法,需要绝对路径. 获取文件的绝对路径:Server.MapPath(相对路径); 或许要上传文件的本身名字用.FileNam ...
- R语言-直方图
1.直方图 直方图和柱形图的区别:直方图表示频数,柱形图表示数量. 一般直方图的X轴表示取值范围,Y轴表示频数 hist() 函数 > hist(rnorm(1000)) #1000个正态随机数 ...
- KADEMLIA算法学习
在上一篇文章中<P2P技术是什么>,我们介绍了P2P技术的特点以及发展历史.在本篇文章中,我们来介绍某一个具体的算法. 如今很多P2P网络的实现都采用DHT的方式实现查找,其中Kademl ...
- php json中文被转义
php 5.4 json_encode($str, JSON_UNESCAPED_UNICODE); 5.4版本以下 方法一function encode_json($str){ $code = js ...
- 1.5.7、CDH 搭建Hadoop在安装之前(定制安装解决方案---配置单用户模式)
配置单用户模式 在传统的Cloudera Manager部署中,管理每台主机上的Hadoop进程的Cloudera Manager Agent以root用户身份运行.但是,某些环境会限制对root帐户 ...
- 创建java项目思路
一.搭建 1.创建搭建项目 2.创建分层 二.理解项目(理清总体思路) 1.是否有共同部分(过滤或者拦截) 常用量 (static) 2.搭建单表基本增(是否需要返回值) 删(条件) 查(条 ...
- 老代码:js实现二级城市联动(MVC)
FormViewCity 为mvc控制器传给view的数据,包括一个MyCitys集合字段. <%@ Page Title="" Language="C#" ...