【题目链接】:http://codeforces.com/contest/798/problem/B

【题意】



给你n个字符串;

每次操作,你可以把字符串的每个元素整体左移(最左边那个字符跑到最后面去了)

问你最少经过多少次操作可以使得所有字符串都相同;

【题解】



枚举最后每个字符串都变成了哪一个字符串;

然后每个字符串都模拟一下左移的过程;直到相等记录总的移动次数;

或者左移超过了长度的次数;输出不可能能和目标串一样;

记录最小的移动次数就好;



【Number Of WA】



0



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 110;
const int INF = 21e8; int n,ans=INF;
string s[N],s1[N]; int ok(string t)
{
rep1(i,1,n)
s1[i]=s[i];
int temp = 0,len = s1[1].size();
rep1(i,1,n)
{
int cnt = 0;
while (s1[i]!=t)
{
char t = s1[i][0];
s1[i].erase(0,1);
s1[i]+=t;
cnt++;
if (cnt>len+2) return INF;
}
temp+=cnt;
}
return temp;
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf�ͱ�����!
cin >> n;
rep1(i,1,n) cin >> s[i];
rep1(i,1,n)
{
int ju = ok(s[i]);
ans = min(ans,ju);
}
if (ans>=INF)
cout <<-1<<endl,0;
else
cout << ans << endl;
return 0;
}

【codeforces 798B】Mike and strings的更多相关文章

  1. 【codeforces 798A】Mike and palindrome

    [题目链接]:http://codeforces.com/contest/798/problem/A [题意] 让你严格改变一个字符,使得改变后的字符串为一个回文串; 让你输出可不可能; [题解] 直 ...

  2. 【codeforces 798C】Mike and gcd problem

    [题目链接]:http://codeforces.com/contest/798/problem/C [题意] 给你n个数字; 要求你进行若干次操作; 每次操作对第i和第i+1个位置的数字进行; 将 ...

  3. 【codeforces 798D】Mike and distribution

    [题目链接]:http://codeforces.com/contest/798/problem/D [题意] 让你选一个下标集合 p1,p2,p3..pk 使得2*(a[p1]+a[p2]+..+a ...

  4. 【31.58%】【codeforces 682D】Alyona and Strings

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  6. 【24.34%】【codeforces 560D】Equivalent Strings

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. 【52.49%】【codeforces 556A】Case of the Zeros and Ones

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  8. 【19.46%】【codeforces 551B】ZgukistringZ

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 【codeforces 755B】PolandBall and Game

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

随机推荐

  1. 【iOS】网络载入图片缓存与SDWebImage

    载入网络图片能够说是网络应用中必备的.假设单纯的去下载图片,而不去做多线程.缓存等技术去优化,载入图片时的效果与用户体验就会非常差. 一.自己实现载入图片的方法 tips: *iOS中全部网络訪问都是 ...

  2. ubuntu系统启动qtceator时提示:Qt5.5.1/Tools/QtCreator/lib/qtcreator/plugins/libHelp.so: 无法加载库

    在ubuntu系统下安装好qt5.5后启动qtceator时提示:Qt5.5.1/Tools/QtCreator/lib/qtcreator/plugins/libHelp.so: 无法加载库Qt5. ...

  3. u-boot的内存分布和全局数据结构

    U-boot,除非在RAM中调试,一般情况下都是从flash中执行一段代码,然后将flash中储存的代码和数据搬移到ram中,然后跳转到ram中执行.当然这应该也是一般的bootloader的执行方式 ...

  4. fontSpider字蛛,好用的字体压缩工具教程

    一直觉得很多字体特别好看,但是那些好看的字体只能做在图片上不能用CSS样式去实现,作为一个会设计的前端,真心觉得很烦恼,有时候那些文字需要更换,修改起来非常麻烦,要到处去找源文件,找不到源文件还要尽力 ...

  5. javascript中计算点击多少次

    点击事件:onclick <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  6. 删除git上已经提交的文件

    1.先查看有哪些文件可以删除,但是不真执行删除 git rm -r -n job-executor-common/target/* -r  递归移除目录 -n 加上这个参数,执行命令时,是不会删除任何 ...

  7. c#使用RSA进行注册码验证

    公司的一个项目快完成了,最后要加上注册验证,翻了n多资料,终于做出来了.现在把体验说一下,以后要用的时候也好找.~~ .Net自带的类库里面有个算法. 这个算法的原理是不对称加密的原理.不对称加密原理 ...

  8. php实现非对称加密

    <?php /** * 使用openssl实现非对称加密 * * @since 2015-11-10 */ class Rsa { /** * 私钥 * */ private $_privKey ...

  9. Ceph 文件系统的安装

    yum install -y wget wget https://pypi.python.org/packages/source/p/pip/pip-1.5.6.tar.gz#md5=01026f87 ...

  10. (转)基于Metronic的Bootstrap开发框架经验总结(2)--列表分页处理和插件JSTree的使用

    http://www.cnblogs.com/wuhuacong/p/4759564.html 在上篇<基于Metronic的Bootstrap开发框架经验总结(1)-框架总览及菜单模块的处理& ...