博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #410 (Div. 2)B. Mike and strings(暴力)
阅读量:4986 次
发布时间:2019-06-12

本文共 1953 字,大约阅读时间需要 6 分钟。

Description

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.

Sample Input

4 xzzwo zwoxz zzwox xzzwo
2 molzv lzvmo
3 kc kc kc
3 kc kc kc

Sample Output

5 2 0 -1

思路

题解:

数据范围很小,因此直接暴力求解。

 

#include
using namespace std;const int INF = (1<<30)-1;const int maxn = 55;char str[maxn][maxn];char tmp[maxn],tmp1[maxn];int main(){ //freopen("input.txt","r",stdin); int n,res = INF; bool flag = false,success = true; scanf("%d",&n); for (int i = 0;i < n;i++) { scanf("%s",str[i]); } int len = strlen(str[0]); for (int i = 0;i < n;i++) { int cnt = 0; for (int j = 0;j < n;j++) { if (i == j) { flag = true; continue; } flag = false; if (strcmp(str[j],str[i]) == 0) { flag = true; continue; } strcpy(tmp1,str[j]); int tmplen = len; while (--tmplen) { strncpy(tmp,tmp1+1,len-1); tmp[len - 1] = tmp1[0]; cnt++; strcpy(tmp1,tmp); if (strcmp(tmp1,str[i]) == 0) { flag = true; break; } } if (!flag) { success = false; break; } } if (!flag) { success = false; break; } res = min(res,cnt); } if (!success) printf("-1\n"); else printf("%d\n",res); return 0;}

  

转载于:https://www.cnblogs.com/ZhaoxiCheung/p/6751758.html

你可能感兴趣的文章
java 线程协作 join()
查看>>
微信群价值塑造方法
查看>>
golang学习笔记15 golang用strings.Split切割字符串
查看>>
jQuery页面滚动图片等元素动态加载实现
查看>>
Html5 iphone - Boot Page
查看>>
Mongodb01 - Mongodb安装与配置
查看>>
深入理解对象的引用
查看>>
starUML破解-version2.8.0已验证
查看>>
selenium实战学习第一课
查看>>
马后炮之12306抢票工具(三) -- 查票(监控)
查看>>
198. House Robber Java Solutions
查看>>
Java_基础篇(杨辉三角)
查看>>
__str__ __repr__ 与 __format__
查看>>
【LoadRunner】loadrunner常见问题汇总
查看>>
css 不换行省略号
查看>>
BZOJ4001 TJOI2015概率论(生成函数+卡特兰数)
查看>>
BZOJ4078 WF2014Metal Processing Plant(二分答案+2-SAT)
查看>>
阿里云宕机故障 - 思考如何保障系统的稳定性
查看>>
selenium(Python)总结
查看>>
腾迅股票数据接口
查看>>