• 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 }
  • 相关阅读:
    损失函数
    DPM 目标检测1
    编程题
    枚举型和元类
    python 多继承
    网络基础Cisco路由交换一
    网络基础tcp/ip协议五
    网络基础tcp/ip协议四
    网络基础tcp/ip协议三
    网络基础tcp/ip协议二
  • 原文地址:https://www.cnblogs.com/yinghualuowu/p/6750344.html
Copyright © 2020-2023  润新知