zoukankan      html  css  js  c++  java
  • Codeforces Round #410 (Div. 2) B

    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.

    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 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define ll long long
     4 const int maxn=54321;
     5 int n;
     6 string s[100];
     7 set<int>q;
     8 set<string>p;
     9 map<string,int>p1,p2;
    10 int main()
    11 {
    12     cin>>n;
    13     for(int i=0;i<n;i++)
    14     {
    15         cin>>s[i];
    16         q.insert(s[i].size());
    17         p.insert(s[i]);
    18     }
    19     if(q.size()!=1)
    20     {
    21         cout<<"-1"<<endl;
    22         return 0;
    23     }
    24     if(p.size()==1)
    25     {
    26         cout<<"0"<<endl;
    27         return 0;
    28     }
    29     for(int i=0;i<n;i++)
    30     {
    31         map<string,int>l;
    32         l.clear();
    33         for(int j=0;j<s[0].size();j++)
    34         {
    35             string ss="";
    36             for(int z=0;z<s[0].size();z++)
    37             {
    38                 ss+=s[i][(j+z)%s[0].size()];
    39             }
    40             l[ss]++;
    41           //  cout<<l[ss]<<" "<<ss<<endl;
    42             if(l[ss]>=2) continue;
    43             p1[ss]+=j;
    44             p2[ss]++;
    45         }
    46        // cout<<endl;
    47     }
    48    // cout<<"A"<<endl;
    49     int Max=(1<<31)-1;
    50     string pos;
    51     for(auto it:p1)
    52     {
    53         pos=it.first;
    54         Max=min(Max,p1[pos]);
    55         if(p2[pos]!=n)
    56         {
    57             cout<<"-1"<<endl;
    58             return 0;
    59         }
    60     }
    61     cout<<Max<<endl;
    62     return 0;
    63 }
  • 相关阅读:
    【解决火车轮播图小圆点跳的问题】传统轮播图-三位法
    jq龙禧轮播图
    QT MSVC环境中添加QWT
    XDMA ip core的使用
    PCIe基础知识与例程分析----PIO_demo
    Day04 (四)_TCP文件传输设计
    Day04 (三)_UDP传输端设计
    Day04 (二)_TCP传输客户器端设计
    Day04 (一)_TCP传输服务器端设计
    Day03 (下)_Qt文件系统
  • 原文地址:https://www.cnblogs.com/yinghualuowu/p/6750344.html
Copyright © 2011-2022 走看看