zoukankan      html  css  js  c++  java
  • CF410div2 B. Mike and strings

     1 /*
     2  CF410div2 B. Mike and strings
     3  http://codeforces.com/contest/798/problem/B
     4  字符串 暴力
     5  题意:给你n个串,每次操作可以将某个串的第一个字符放到最后去,
     6  问最少的次数,使得所有字符串都相同
     7  思路:先将所有字符串复制成二倍,然后暴力枚举要变成的串
     8  注意数组都要开二倍。。
     9  */
    10 #include <cstdio>
    11 #include <algorithm>
    12 #include <cstring>
    13 #include <cmath>
    14 #include <vector>
    15 #include <queue>
    16 #include <iostream>
    17 #include <map>
    18 #include <set>
    19 //#define test
    20 using namespace std;
    21 const int Nmax=105;//空间记得要开两倍
    22 char s[Nmax][Nmax];
    23 char tmp[Nmax];
    24 int ans[Nmax][Nmax];
    25 int n,len=0;
    26 int check(int a,int b)
    27 {
    28     for(int i=b;i<=b+len-1;i++)
    29     {
    30         if(tmp[i-b+1]!=s[a][i])
    31             return 0;
    32     }
    33     return 1;
    34 }
    35 int main()
    36 {
    37     #ifdef test
    38     #endif
    39     //freopen("2.in","r",stdin);
    40     scanf("%d",&n);
    41     for(int i=1;i<=n;i++)
    42     {
    43         scanf("%s",s[i]+1);
    44         //printf("%s
    ",s[i]+1);
    45         len=max(len,(int)strlen(s[i]+1));
    46         for(int j=len+1;j<=len<<1;j++)
    47             s[i][j]=s[i][j-len];
    48     }
    49     for(int i=1;i<=len;i++)//枚举匹配头
    50     {
    51         int k=i;
    52         for(int j=1;j<=len;j++)
    53             tmp[j]=s[1][k++];
    54         for(int j=2;j<=n;j++)
    55         {
    56             int flag=1;
    57             for(int t=1;t<=len;t++)
    58             {
    59                 if(check(j,t))
    60                 {
    61                     ans[j][i]=t-1;
    62                     //printf("ans[%d][%d]=%d
    ",j,i,ans[j][i]);
    63                     flag=0;
    64                     break;
    65                 }
    66             }
    67             if(flag)
    68             {
    69                 printf("-1
    ");
    70                 return 0;
    71             }
    72         }
    73     }
    74     for(int i=1;i<=len;i++)
    75         ans[1][i]=i-1;
    76     int res=1e9,book=0;
    77     for(int t=1;t<=len;t++)
    78     {
    79         book=0;
    80         for(int i=1;i<=n;i++)
    81             book+=ans[i][t];
    82         //printf("book[%d]:%d
    ",t,book);
    83         res=min(res,book);
    84     }
    85     printf("%d
    ",res);
    86     return 0;
    87 }
  • 相关阅读:
    scrapy下载图片第一波
    scrapy之防ban策略
    xml之dtd约束
    Java jdbc相关
    XSS获取cookie
    反射型XSS & DVWA
    MySQL数据库设计规范
    SQL Server 查询指定时间是一年当中的第几周
    js验证15位或18位身份证
    C# Process.Start()
  • 原文地址:https://www.cnblogs.com/BBBob/p/6751817.html
Copyright © 2011-2022 走看看