zoukankan      html  css  js  c++  java
  • CF-798B

    B. Mike and strings
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    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?

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

    题意:

    将所有字符串变成相等,只允许将最左边的字符移到最右,问最少要移多少步。

    若不能使所有相等,则输出-1、

    分别以每一个字符串为模板,将其他的字符串移成和当前字符串相等的情况,再找出步数最少的方案。

    附AC代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 const int inf=1<<30;
     5 
     6 string s[60];
     7 
     8 int main(){
     9     int n,i,j,k,m;
    10     cin>>n;
    11     for(i=0;i<n;i++){
    12         cin>>s[i];
    13     }
    14     int len=s[0].size();
    15     int ans=inf;
    16     for(i=0;i<n;i++){
    17         int cnt=0;
    18         for(j=0;j<n;j++){
    19             for(m=0;m<len;m++){
    20                 for(k=0;k<len;k++){
    21                     if(s[i][k]!=s[j][(k+m)%len])
    22                     break;
    23                 }
    24                 if(k==len)//只有len个都相等才表明移动m个字符后两字符串相等 
    25                 break;
    26             }
    27             if(m==len){//若m==len则表明不能匹配。 
    28                 cnt=inf;
    29                 break;
    30             }
    31             cnt+=m;
    32         }
    33         ans=min(ans,cnt);
    34     }
    35     if(ans==inf)
    36     cout<<-1<<endl;
    37     else
    38     cout<<ans<<endl;
    39     return 0;
    40 } 
  • 相关阅读:
    页断裂(partial write)与doublewrite技术
    mysql大小写敏感与校对规则
    mysql自增列导致主键重复问题分析。。。
    一次进程hang住问题分析。。。
    教你手工mysql拆库
    mysql online ddl
    Yii2的深入学习--入口文件
    构建自己的PHP框架--抽象Controller的基类
    构建自己的PHP框架--抽象框架的内容
    构建自己的PHP框架--搭建基本结构
  • 原文地址:https://www.cnblogs.com/Kiven5197/p/6770323.html
Copyright © 2011-2022 走看看