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[],temp;
cin>>n;
for(i=;i<=n;i++)
cin>>a[i];
ans=;
for(i=;i<=n;i++)
{
t=;
for(j=;j<=n;j++)
{
temp=a[j]+a[j];
if(temp.find(a[i])==string::npos)
{
cout<<-<<endl;
return ;
}
t+=temp.find(a[i]);
}
ans=miner(ans,t);
}
cout<<ans<<endl;
}
 

Codeforces Round #410 (Div. 2) B的更多相关文章

  1. Codeforces Round #410 (Div. 2)

    Codeforces Round #410 (Div. 2) A B略..A没判本来就是回文WA了一次gg C.Mike and gcd problem 题意:一个序列每次可以把\(a_i, a_{i ...

  2. Codeforces Round #410 (Div. 2)C. Mike and gcd problem

    题目连接:http://codeforces.com/contest/798/problem/C C. Mike and gcd problem time limit per test 2 secon ...

  3. Codeforces Round #410 (Div. 2)(A,字符串,水坑,B,暴力枚举,C,思维题,D,区间贪心)

    A. Mike and palindrome time limit per test:2 seconds memory limit per test:256 megabytes input:stand ...

  4. Codeforces Round #410 (Div. 2)A B C D 暴力 暴力 思路 姿势/随机

    A. Mike and palindrome time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  5. Codeforces Round #410 (Div. 2) A. Mike and palindrome

    A. Mike and palindrome time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  6. Codeforces Round #410 (Div. 2) A

    Description Mike has a string s consisting of only lowercase English letters. He wants to change exa ...

  7. Codeforces Round #410 (Div. 2) A. Mike and palindrome【判断能否只修改一个字符使其变成回文串】

    A. Mike and palindrome time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  8. Codeforces Round #410 (Div. 2)D题

    D. Mike and distribution time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  9. Codeforces Round #410 (Div. 2)C题

    C. Mike and gcd problem time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

随机推荐

  1. I2S音频总线学习

    IIS音频总线学习(一)数字音频技术 一.声音的基本概念 声音是通过一定介质传播的连续的波. 图1 声波 重要指标: 振幅:音量的大小 周期:重复出现的时间间隔 频率:指信号每秒钟变化的次数 声音按频 ...

  2. VSCode Install Go

    首先是VScode官网下载:https://code.visualstudio.com/ 然后在电脑上安装go的环境 如下图在VSCode上搜go的插件进行安装: 推荐:vscode-icons这个插 ...

  3. 有哪些操作会使用到TempDB;如果TempDB异常变大,可能的原因是什么,该如何处理(转载)

    有哪些操作会使用到TempDB:如果TempDB异常变大,可能的原因是什么,该如何处理:tempdb的用途: 存储专用和全局临时变量,不考虑数据库上下文: 与Order by 子句,游标,Group ...

  4. js求数组的最大值--奇技淫巧和笨方法

    写这篇文章的原因 我目前做的项目很少用到算法,于是这方面的东西自然就有点儿生疏.最近的一次编码中遇到了从数组中获取最大值的需求,当时我不自觉的想到了js的sort()函数,现在想来真是有些“罪过”,当 ...

  5. CF633H Fibonacci-ish II 莫队、线段树、矩阵乘法

    传送门 这题除了暴力踩标程和正解卡常数以外是道很好的题目 首先看到我们要求的东西与\(Fibonacci\)有关,考虑矩阵乘法进行维护.又看到\(n \leq 30000\),这告诉我们正解算法其实比 ...

  6. 安卓自动化测试,贺晓聪之uiautomator设备和选择器~Python详解

    1.设备对象 引入uiautomator,获取设备对象<所谓设备对象可理解为:Android模拟器或者真机> 语法:from uiautomator import device as d ...

  7. electron 开发实时加载

    第一个方式 cnpm install electron-reload --save-dev cnpm install electron-prebuilt --save-dev require('ele ...

  8. jenkins 构建后发送钉钉消息通知(插件)

    钉钉,越来越多的公司采用,那么我们在持续集成中,也可以直接选择钉钉插件的,在之前的博客中 ,对发送的钉钉消息进行了定制,那样的话会开启一个新的任务, 其实今天呢,我们可以直接安装一个插件就可以发送了, ...

  9. Jmeter(三十五)_精确实现网页爬虫

    Jmeter实现了一个网站文章的爬虫,可以把所有文章分类保存到本地文件中,并以文章标题命名 它原理就是对网页提交一个请求,然后把返回的所有值提取出来,利用ForEach控制器去实现遍历.下面来介绍一下 ...

  10. 手动编写的几个简单的puppet管理配置

    puppet在自动化配置管理方面有很强大的优势,这里就不做过多介绍了,下面记录下几个简单的puppet管理配置: 一.首先在服务端和客户端安装puppet和facter 1)服务端 安装Puppet ...