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 ...
随机推荐
- 吴裕雄 05-mysql删除数据库
drop database <数据库名>; 例如删除名为 RUNOOB 的数据库:drop database RUNOOB; 使用 mysqladmin 删除数据库你也可以使用 mysql ...
- spark pyspark 常用算法实现
利用Spark-mllab进行聚类,分类,回归分析的代码实现(python) http://www.cnblogs.com/adienhsuan/p/5654481.html 稀疏向量: 关于Spar ...
- The CHAR and VARCHAR Types
[The CHAR and VARCHAR Types] The CHAR and VARCHAR types are declared with a length that indicates th ...
- metasploit framework(十):SSH扫描、爆破
SSH版本扫描 SSH密码爆破 设置爆破字典 run开始
- ELK 日志学习
一.Elasticsearch 安装(所有的版本均使用5.5.0 ,且版需要相同 logstash \ kibana \ filebeat ) 官网下载地址:https://www.elastic.c ...
- 【python】安装pymongo时出错
在python2.7的环境下,使用pip install pymongo安装模块报以下错误: Could not install packages due to an EnvironmentError ...
- artTemplate js模板引擎动态给html赋值
html放到$("#area").append(html);之前,否则文档流获取不到#area <table width="90%" class=&quo ...
- 关于slavetable
slavetable有两种情况, 从表有三个要素 1.自己主键字段的idfield 2.对应主表的主键字段masterIdField 3.对应主表主键的值 模式一.MasetrEdit模式 也就是主 ...
- hdoj1013(数根,大数,九余数算法)
Digital Roots Problem Description The digital root of a positive integer is found by summing the dig ...
- Centos 7 RabbitMQ + Haproxy 集群高可用部署
一. 功能和原理介绍 RabbitMQ是一个开源的AMQP实现,服务器端用Erlang语言编写,支持多种客户端,如:Python.Ruby..NET.Java.JMS.C.PHP.ActionSc ...