【题目链接】: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. [面试题]java中final finally finalized 的差别是什么?

    final 是修饰符,能够用于修饰变量.方法和类.修饰变量时.代表变量不能够改动,也就是常量了.常量须要在定义时赋值或通过构造函数赋值,两者仅仅能选其一:修饰方法时,代表方法仅仅能调用,不能被 ove ...

  2. Codeforces Round #332 (Div. 2)A. Patrick and Shopping 水

    A. Patrick and Shopping   Today Patrick waits for a visit from his friend Spongebob. To prepare for ...

  3. luogu2770 航空路线问题 网络流

    题目大意: 给定一张航空图,图中顶点代表城市,边代表 2 城市间的直通航线.现要求找出一条满足下述限制条件的且途经城市最多的旅行路线.(1)从最西端城市出发,单向从西向东途经若干城市到达最东端城市,然 ...

  4. 微信小程序初探(一、简单的数据请求)

    微信小程序出来有一段时间了,之前没看好小程序(觉得小程序体验不咋好,内心对新事物有抵触心里,请原谅我的肤浅[捂脸][捂脸]),不过后来偶然之间玩过小程序的游戏(跳一跳.球球大作战.猜画小歌 等),顿悟 ...

  5. Sqlserver 数据库恢复常见错误及解决(网站转载 留着备用)

    数据库恢复常见错误及解决 2009-04-13 11:25 1145人阅读 评论(0) 收藏 举报 数据库databasesqlserverusermicrosoftsql server 在sqlSe ...

  6. [Apple开发者帐户帮助]五、管理标识符(2)启用应用服务

    您可以在证书,标识符和配置文件中查看和启用App ID的服务.包含已修改的App ID的供应配置文件将变为无效.您需要重新生成使用该App ID的配置文件. 注意:要为应用程序完全配置服务,请在Xco ...

  7. lowbit( )运算

    --------开始-------- lowbit (n) 定义为非负整数n在二进制表示下“最低位的1及其后面所有的0构成的数值. 比如: n = 10 的二进制下为1010,则lowbit (n) ...

  8. 3Ddungeon-------三维搜索-----偷个懒 把 亡命逃窜 的代码修改了一下 拿来用了

    题 很简单  就是给一个   三维的迷宫然后 开你起始地点 S 问你能不能到达 出口 E 能的话 需要多长时间 ? #include<stdio.h> #include<string ...

  9. java解析注解的简单例子

    代码是根据慕课网的教程写的. 自定义类的注解: package com.immoc.test; import java.lang.annotation.Documented; import java. ...

  10. oracle 命令记录

    监听程序启动停止查看名利: 1.切换到oracle用户:su - oracle 2.查看监听状态:lsnrctl status 3.停止监听:lsnrctl stop 4.启动监听:lsnrctl s ...