【codeforces 798B】Mike and strings
【题目链接】: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的更多相关文章
- 【codeforces 798A】Mike and palindrome
[题目链接]:http://codeforces.com/contest/798/problem/A [题意] 让你严格改变一个字符,使得改变后的字符串为一个回文串; 让你输出可不可能; [题解] 直 ...
- 【codeforces 798C】Mike and gcd problem
[题目链接]:http://codeforces.com/contest/798/problem/C [题意] 给你n个数字; 要求你进行若干次操作; 每次操作对第i和第i+1个位置的数字进行; 将 ...
- 【codeforces 798D】Mike and distribution
[题目链接]:http://codeforces.com/contest/798/problem/D [题意] 让你选一个下标集合 p1,p2,p3..pk 使得2*(a[p1]+a[p2]+..+a ...
- 【31.58%】【codeforces 682D】Alyona and Strings
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【24.34%】【codeforces 560D】Equivalent Strings
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【52.49%】【codeforces 556A】Case of the Zeros and Ones
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【19.46%】【codeforces 551B】ZgukistringZ
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 755B】PolandBall and Game
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
随机推荐
- C#获取本机Sql Serverserver名
private void Form2_Load(object sender, EventArgs e) { listBox1.Items.Clear(); SQLDMO.Application SQL ...
- oc39-- 类的内存存储
虚线是isa的指向,实线是继承关系. // // main.m // 类的本质 #import <Foundation/Foundation.h> #import "Person ...
- How to Integrate .NET Projects with Jenkins
https://www.swtestacademy.com/jenkins-dotnet-integration/ 8) Unit Tests and Test Coverage Settings D ...
- AE错误代码解释
每当我们在进行AE开发,出现错误时经常会出现错误代码,但是我们并不知道它到底代表什么意思,这里的而错误编码我们可以对照着找到我们需要的时候常详细信息(问题是,经常还是会出现没有错误编码HRESULT ...
- [LnOI2019]长脖子鹿省选模拟赛 东京夏日相会
这里来一发需要开毒瘤优化,并且几率很小一遍过的模拟退火题解... 友情提醒:如果你很久很久没有过某一个点,您可以加上特判 可以像 P1337 [JSOI2004]平衡点 / 吊打XXX 那道题目一样 ...
- python利用有道翻译实现“语言翻译器”的功能
import urllib.request import urllib.parse import json while True: content = input('请输入需要翻译的内容(退出输入Q) ...
- POJ 2342 Anniversiry Party(TYVJ1052 没有上司的舞会)
题意: P1052 没有上司的舞会 描述 Ural大学有N个职员,编号为1~N.他们有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.每个职员有一个快乐指数.现在有个周 ...
- Elasticsearch之curl删除
扩展下, Elasticsearch之curl删除索引库 [hadoop@djt002 elasticsearch-2.4.3]$ curl -XDELETE 'http://192.168.80.2 ...
- .Net Core 学习(二)上篇
用Visual Studio和ASP.NET Core MVC创建Web API 1.启动Visual Studio.从文件菜单,选择新建>项目.选择ASP.NET Core Web Appli ...
- SQlserver 当输入参数为可选条件
以前很懒,都是用拼接字符串的方式,加上if 语句,根据输入参数是否为空来判断是否需要在where 后加上对应字段的条件限制 但是拼接字符串很烦,又总是被转义符搞得很烦 '''' 所以想了其他办法 分 ...