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;
}

 

 

转载于:https://www.cnblogs.com/kls123/p/6804385.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值