传送门

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

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

Sample Output

  1. 5
  2.  
  3. 2
  4.  
  5. 0
  6.  
  7. -1

思路

题解:

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

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int INF = (1<<30)-1;
  4. const int maxn = 55;
  5. char str[maxn][maxn];
  6. char tmp[maxn],tmp1[maxn];
  7.  
  8. int main()
  9. {
  10. //freopen("input.txt","r",stdin);
  11. int n,res = INF;
  12. bool flag = false,success = true;
  13. scanf("%d",&n);
  14. for (int i = 0;i < n;i++)
  15. {
  16. scanf("%s",str[i]);
  17. }
  18. int len = strlen(str[0]);
  19. for (int i = 0;i < n;i++)
  20. {
  21. int cnt = 0;
  22. for (int j = 0;j < n;j++)
  23. {
  24. if (i == j)
  25. {
  26. flag = true;
  27. continue;
  28. }
  29. flag = false;
  30. if (strcmp(str[j],str[i]) == 0)
  31. {
  32. flag = true;
  33. continue;
  34. }
  35. strcpy(tmp1,str[j]);
  36. int tmplen = len;
  37.  
  38. while (--tmplen)
  39. {
  40. strncpy(tmp,tmp1+1,len-1);
  41. tmp[len - 1] = tmp1[0];
  42. cnt++;
  43. strcpy(tmp1,tmp);
  44. if (strcmp(tmp1,str[i]) == 0)
  45. {
  46. flag = true;
  47. break;
  48. }
  49. }
  50. if (!flag)
  51. {
  52. success = false;
  53. break;
  54. }
  55. }
  56. if (!flag)
  57. {
  58. success = false;
  59. break;
  60. }
  61. res = min(res,cnt);
  62. }
  63. if (!success) printf("-1\n");
  64. else printf("%d\n",res);
  65. return 0;
  66. }

  

Codeforces Round #410 (Div. 2)B. Mike and strings(暴力)的更多相关文章

  1. 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 ...

  2. Codeforces Round #410 (Div. 2)A B C D 暴力 暴力 思路 姿势/随机

    A. Mike and palindrome time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  3. Codeforces Round #410 (Div. 2)C. Mike and gcd problem

    题目连接:http://codeforces.com/contest/798/problem/C C. Mike and gcd problem time limit per test 2 secon ...

  4. Codeforces Round #410 (Div. 2) A. Mike and palindrome

    A. Mike and palindrome time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  5. Codeforces Round #410 (Div. 2) A. Mike and palindrome【判断能否只修改一个字符使其变成回文串】

    A. Mike and palindrome time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  6. 【推导】Codeforces Round #410 (Div. 2) C. Mike and gcd problem

    如果一开始就满足题意,不用变换. 否则,如果对一对ai,ai+1用此变换,设新的gcd为d,则有(ai - ai+1)mod d = 0,(ai + ai+1)mod d = 0 变化一下就是2 ai ...

  7. Codeforces Round #305 (Div. 2) B. Mike and Fun 暴力

     B. Mike and Fun Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/548/pro ...

  8. Codeforces Round #305 (Div. 2) A. Mike and Fax 暴力回文串

     A. Mike and Fax Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/548/pro ...

  9. Codeforces Round #305 (Div. 1) A. Mike and Frog 暴力

     A. Mike and Frog Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/547/pr ...

随机推荐

  1. 搜索(DFS)---填充封闭区域

    填充封闭区域 130. Surrounded Regions (Medium) For example, X X X X X O O X X X O X X O X X After running y ...

  2. C# EF优化

    原文:https://www.cnblogs.com/wangyuliang/p/10338902.html   https://www.cnblogs.com/simadi/p/6879366.ht ...

  3. frontend-dev面试

    1.笔试题 vuex 存储的数据为null或者undefined是为啥? 1.伸缩布局 flex规则 2.横向布局的实现方法有多少? 3.说一说 flex:1; 的含义 / 说一说flex:1 1 3 ...

  4. JavaScript 的执行机制

    一.关于javascript javascript是一门单线程语言,在最新的HTML5中提出了Web Worker,但javascript是单线程这一核心仍未改变. 为什么js是单线程的语言?因为最初 ...

  5. 【错误】mysql 出现 "1067 - Invalid default value for 'UPDATE_TIME' " 错误提示的解决办法

    今天工作中遇到修改表结构的时候出现错误 Invalid default value for 'UPDATE_TIME 问题原因是因为db 表中update_time的默认时间写成了 '0000-00- ...

  6. 日志处理--高效Linux命令整理

    序 在学习使用python处理日志开始阶段,对我阻力最大的莫过于对linux的不熟悉了,有种寸步难行的感觉. 在之后乱学一通之后,发现有点对我颇有益处: 学<鸟哥linux私房菜基础学习篇> ...

  7. nbench

    http://www.math.utah.edu/~mayer/linux/bmark.html nbench-byte-2.2.3  --> http://www.math.utah.edu/ ...

  8. centos6 安装mysql5.77(开发版)

    1. 配置yum源: [root@yyf ~]#rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm [ ...

  9. $LCT$维护子树信息学习笔记

    \(LCT\)维护子树信息学习笔记 昨天\(FDF\)好题分享投了 \([ZJOI2018]\)历史 这题. 然后我顺势学学这个姿势. 结果调了一年...于是写个笔记记录一下. 基本原理 比较显然地, ...

  10. flask之url_for函数

    一:url_for函数 干什么的?传入函数名,得到函数的路由地址(访问视图函数的地址) from flask import Flask from flask import url_for app = ...