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

最新文章

  1. SPSS数据分析—Poisson回归模型
  2. 找到多个与名为“Login”的控制器匹配的类型
  3. Android开发-API指南-&lt;data&gt;
  4. Oracle 热备份batch脚本 Windows
  5. per-project basis
  6. android - android Couldn&#39;t load runtimecore_java from loader
  7. Android日志框架darks-logs使用教程
  8. 3步学会用gulp
  9. Cocos2D-X扫盲之坐标系、锚点
  10. 文件搜索神器 Everything
  11. 设计模式--装饰者设计模式(Decorator)
  12. C# Process.Start()
  13. Maven01——简介、安装配置、入门程序、项目构建和依赖管理
  14. C#中泛型之Dictionary
  15. wamp环境下如何安装redis扩展
  16. Node.js调用C#代码
  17. STM32-正弦波可调(50HZ~20KHZ可调、峰峰值0~3.3V可调)
  18. spring jpa方法关键字转成sql
  19. mybatis foreach报错It was either not specified and/or could not be found for the javaType Type handler
  20. input type=file 上传文件样式美化(转载)

热门文章

  1. [01] Collection和Map
  2. awk 内置函数列表
  3. Webpack 概念
  4. OpenStack报错:MessagingTimeout: Timed out waiting for a reply to message ID
  5. jdk8+Mybatis3.5.0+Mysql读取LongBlob失败
  6. CF 958E2. Guard Duty (medium)
  7. IO复用\阻塞IO\非阻塞IO\同步IO\异步IO
  8. 懒人小工具1:winform自动生成Model,Insert,Select,Delete以及导出Excel的方法
  9. Jmeter(三十二)_搭建本地接口自动化环境
  10. hadoop-mapreduce-(1)-统计单词数量