• Codeforces Round #410 (Div. 2) B


    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".
     
    题解:
    之前打的时候代码不严谨,写的太混乱,各种出问题,改起来还很麻烦,后面看了下其他大佬的代码风格,重新打了一遍,代码看起来清晰很多,而且不容易犯错。
    这道题还是比较简单的,主要是循环取其中一个与其他的比较,并记录需要的步数,最后进行比较,得出最小的步数
     
    实现代码:
     #include<bits/stdc++.h>


    using namespace std;
    typedef long long LL;

    const int N = 55;
    const int INF = 0x3f3f3f3f;

    int n;
    string S[N];

    int Compute(string T,string S)
    {
        for(int i=0;i<S.size();i++)
        {
            string W = "";
            for(int j = i;j < S.size();++j)
            W += S[j];
            for(int j = 0; j<=i-1 ;++j)
            W += S[j];
            if(W == T)
                return i;
        }
        return INF;
    }

    int Check(string T)
    {
        LL ans = 0;
        for(int i = 2;i <= n;++i)
            ans += (LL)Compute(T,S[i]);
        return ans >= INF ? INF : ans;
    }

    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=n;++i)
        cin>>S[i];
        int ans = INF;
        for(int i=0;i<S[1].size();++i)
        {
            string T = "";
            for(int j = i;j < S[1].size();++j)
                T += S[1][j];
            for(int j = 0;j <= i-1;++j)
                T += S[1][j];
            ans = min(ans,Check(T)+i);
        }
        printf("%d", ans >= INF ? -1 : ans);
        return 0;
    }

     新发现了个黑科技解法,巨tm简洁,简直不讲道理

    #include<iostream>
    #include<cstring>
    using namespace std;
    
    int miner(int x,int y){return x<y?x:y;
    }
    
    int main()
    {
        int n,i,j,ans,t;
        string a[55],temp;
        cin>>n;
        for(i=1;i<=n;i++)
        cin>>a[i];
        ans=999999;
        for(i=1;i<=n;i++)
        {
            t=0;
            for(j=1;j<=n;j++)
            {
                temp=a[j]+a[j];
                if(temp.find(a[i])==string::npos)
                {
                    cout<<-1<<endl;
                    return 0;
                }
                t+=temp.find(a[i]);
            }
            ans=miner(ans,t);
        }
        cout<<ans<<endl;
    }
     
  • 相关阅读:
    查询内容在网页里面分页显示+跳页查看
    struts2——通配符
    JavaScript 输出
    oracl函数
    oracle基本用法
    Java之线程处理之二
    java之多线程之一/序列化和反序列化
    错题整理之二
    S2结业考试的第一次测验
    cookie与session的爱恨情仇
  • 原文地址:https://www.cnblogs.com/kls123/p/6804385.html
Copyright © 2020-2023  润新知