B. Mike and strings
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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?

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
4
xzzwo
zwoxz
zzwox
xzzwo
output
5
input
2
molzv
lzvmo
output
2
input
3
kc
kc
kc
output
0
input
3
aa
aa
ab
output
-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”.

问题链接:CodeForces - 798B Mike and strings
问题简述:(略)
问题分析:字符串循环比较问题,按理说应该用字符串循环比较来实现,时间上会比较快。字符串循环比较可以通过模除计算来实现。一个简易的处理是把同一字符串连起来替代字符串比较,用string类来处理是比较方便的。
把这个程序的string方法(函数)find(),用自己编写的函数来实现,可以做出一个高效的程序。
AC的C++语言程序如下:

/* CodeForces - 798B Mike and strings */#include <bits/stdc++.h>using namespace std;const int INF = 0x3F3F3F3F;
const int N = 50;
string s[N];int find(string &a, string &b)
{int len = a.length();for (int i = 0; i < len; i++) {int j;for (j = 0; j < b.length(); j++)if(a[(i + j) % len] != b[j]) break;if(j == b.length()) return i;}return -1;
}int main()
{int n;cin >> n;for (int i = 0; i < n; i++) cin >> s[i];int flag = 1, ans = INF;for (int i = 0; i < n && flag; i++) {int sum = 0, pos;for (int j = 0; j < n; j++) {if ((pos = find(s[j], s[i])) == -1) {flag = 0;break;} elsesum += pos;}if (flag) ans = min(ans, sum);}if (flag) cout << ans << endl;else cout << -1 << endl;return 0;
}

AC的C++语言程序如下:

/* CodeForces - 798B Mike and strings */#include <bits/stdc++.h>using namespace std;const int INF = 0x3F3F3F3F;
const int N = 50;
string s[N], s2[N];int main()
{int n;cin >> n;for (int i = 0; i < n; i++) {cin >> s[i];s2[i] = s[i] + s[i];}int flag = 1, ans = INF;for (int i = 0; i < n && flag; i++) {int sum = 0, pos;for (int j = 0; j < n; j++)if ((pos = s2[j].find(s[i])) == string::npos) {flag = 0;break;} elsesum += pos;if (flag) ans = min(ans, sum);}if (flag) cout << ans << endl;else cout << -1 << endl;return 0;
}

CodeForces - 798B Mike and strings相关推荐

  1. 798B. Mike and strings

    B. Mike and strings:题目 思路:纯暴力题,不想写,copy了别人代码 #include<iostream> #include<string> using n ...

  2. D - Mike and strings

    D - Mike and strings Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters ...

  3. 【codeforces 798B】Mike and strings

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

  4. CodeForces 689B Mike and Shortcuts (bfs or 最短路)

    Mike and Shortcuts 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/F Description Recently ...

  5. Codeforces 798D Mike and distribution (构造)

    题目链接 http://codeforces.com/contest/798/problem/D 题解 前几天的模拟赛,居然出这种智商题..被打爆了QAQ 这个的话,考虑只有一个序列怎么做,把所有的排 ...

  6. codeforces 596E Wilbur and Strings(DFS)

    题目链接 E. Wilbur and Strings time limit per test 3 seconds memory limit per test 256 megabytes input s ...

  7. Codeforces 149 E. Martian Strings

    正反两遍扩展KMP,维护公共长度为L时,出现在最左边和最右边的位置.... 然后枚举判断... E. Martian Strings time limit per test 2 seconds mem ...

  8. codeforces 689B Mike and Shortcuts 最短路

    题目大意:给出n个点,两点间的常规路为双向路,路长为两点之间的差的绝对值,第二行为捷径,捷径为单向路(第i个点到ai点),距离为1.问1到各个点之间的最短距离. 题目思路:SPFA求最短路 #incl ...

  9. CodeForces - 548D Mike and Feet(单调栈)

    题目链接:点击查看 题目大意:给出一个长度为 n 的数列,现在规定对于任意长度区间为 len 的答案为,所有长度为 len 的区间内的最小值的最大值,题目要求我们输出len为 1 ~ n 时的答案 题 ...

最新文章

  1. 数论计算机科学与技术专业就业前景好,【数学】数学专业就业前景:你看不见的“前途似锦”...
  2. 2k 幻14_ROG幻14肝游戏有何体验?携RTX系列献上终极光追画面福利
  3. celeba数据集_轻松学 Pytorch 使用DCGAN实现数据复制
  4. TensorFlow 教程 --教程--2.2 数据准备
  5. 设计模式学习摘要-抽象工厂
  6. Sharepoint2010 From 认证常见问题
  7. Centos-shell-简介
  8. 软考计算机网络初级试题答案,2015年下半年中级软考《计算机网络—网络工程师》试题及答案...
  9. 时序预测 | MATLAB实现DBN深度置信网络时间序列预测
  10. IPv4到IPv6的改造转换方案(上):IPv6和IPv4优势对比
  11. 项目成败的关键要素:有效沟通
  12. navicat mysql server has gone away_Navicat中MySQL server has gone away错误怎么办
  13. 新浪微博技术架构分析-转载
  14. linux eclipse glib.h,eclipse Glib
  15. 【ArcGIS Pro二次开发】(15):用地用海名称和代码互转
  16. 学习难度最高的五大编程语言,Java排第3,Python竟然都不能上榜
  17. 悲观锁 乐观锁的原理及应用场景
  18. Apache Spark 3.0 GraphX编程指南
  19. 选择所在城市html按字母,微信小程序实现根据字母选择城市功能
  20. 防抖函数:多次触发后只执行第一次或者最后一次的函数

热门文章

  1. Ubuntu硬盘全盘备份tar和clonezilla两种方案
  2. Java多线程(一)——多线程实现方法和生命周期
  3. mysql——时间字段类型与C#中datetime
  4. ArcGIS 10.2.2 for Desktop非管理员权限用户连接Oracle12c,崩溃
  5. 苹果本Win10双系统开启CPU虚拟化
  6. 说服力:从场景化出发的用户价值
  7. mysql数据库创建交叉表查询_sql – 我需要知道如何创建交叉表查询
  8. hive体系架构以及各个组件的作用
  9. Spark Shuffle详解剖析
  10. 集群、分布式、集中式、伪分布式的概念与区别