Codeforces Round #410 (Div. 2)B. Mike and strings(暴力)
Description
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.
Sample Input
4
xzzwo
zwoxz
zzwox
xzzwo
2
molzv
lzvmo
3
kc
kc
kc
3
kc
kc
kc
Sample Output
5 2 0 -1
思路
题解:
数据范围很小,因此直接暴力求解。
#include<bits/stdc++.h>
using namespace std;
const int INF = (1<<30)-1;
const int maxn = 55;
char str[maxn][maxn];
char tmp[maxn],tmp1[maxn]; int main()
{
//freopen("input.txt","r",stdin);
int n,res = INF;
bool flag = false,success = true;
scanf("%d",&n);
for (int i = 0;i < n;i++)
{
scanf("%s",str[i]);
}
int len = strlen(str[0]);
for (int i = 0;i < n;i++)
{
int cnt = 0;
for (int j = 0;j < n;j++)
{
if (i == j)
{
flag = true;
continue;
}
flag = false;
if (strcmp(str[j],str[i]) == 0)
{
flag = true;
continue;
}
strcpy(tmp1,str[j]);
int tmplen = len; while (--tmplen)
{
strncpy(tmp,tmp1+1,len-1);
tmp[len - 1] = tmp1[0];
cnt++;
strcpy(tmp1,tmp);
if (strcmp(tmp1,str[i]) == 0)
{
flag = true;
break;
}
}
if (!flag)
{
success = false;
break;
}
}
if (!flag)
{
success = false;
break;
}
res = min(res,cnt);
}
if (!success) printf("-1\n");
else printf("%d\n",res);
return 0;
}
Codeforces Round #410 (Div. 2)B. Mike and strings(暴力)的更多相关文章
- Codeforces Round #410 (Div. 2) B. Mike and strings
B. Mike and strings time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 【推导】Codeforces Round #410 (Div. 2) C. Mike and gcd problem
如果一开始就满足题意,不用变换. 否则,如果对一对ai,ai+1用此变换,设新的gcd为d,则有(ai - ai+1)mod d = 0,(ai + ai+1)mod d = 0 变化一下就是2 ai ...
- Codeforces Round #305 (Div. 2) B. Mike and Fun 暴力
B. Mike and Fun Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/548/pro ...
- Codeforces Round #305 (Div. 2) A. Mike and Fax 暴力回文串
A. Mike and Fax Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/548/pro ...
- Codeforces Round #305 (Div. 1) A. Mike and Frog 暴力
A. Mike and Frog Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/547/pr ...
随机推荐
- 手写一个SpringMVC框架(转)
一:梳理SpringMVC的设计思路 本文只实现自己的@Controller.@RequestMapping.@RequestParam注解起作用,其余SpringMVC功能读者可以尝试自己实现. 1 ...
- PHP实现简单的万年历
<?php /*********************** *** 功能:万年历 *** *** 时间:2015/05/23 *** ***********************/ //1. ...
- MySQL语句之数据的增删改查
1.插入记录insert语法:INSERT INTO tablename (field1,field2,……fieldn) VALUES(value1,value2,……valuesn); 也可以一次 ...
- JDK_1.8的Windows和Linux环境下的下载与安装
下载: Eclipse需要Jdk,MyEclipse有自带的Jdk 直接点击下载. Windows下JDK安装: 双击运行程序 下一步: 路径 更改到E:\Software\Java\jre1.8.0 ...
- 第三讲JdbcRealm及Authentication Strategy
1.使用shiro框架来完成认证工作,默认情况下使用的是IniRealm.如果需要使用其他Realm,那么需要进行相关的配置. 2.ini配置文件讲解: [main] section是你配置应用程序的 ...
- 关于post xml的请求和响应
关于post的请求作为开发者应该常用到,post请求的数据包含了参数和data,post参数相对比较容易理解,比如我们一个form提交,其实就是调用后台方法的,发送相关参数,这里我单独说一下关于pos ...
- cf2c(模拟退火 步长控制
https://www.luogu.org/problem/CF2C 题意:在平面上有三个没有公共部分的圆,求平面上一点使得到三个圆的切线的夹角相等.(若没答案满足条件,则不打印 思路:可用模拟退火算 ...
- mysql 5.5和5.6版本关于timestamp not null类型字段关于null的处理
Server version: 5.5.33-31.1-log Percona Server (GPL), Release rel31.1, Revision 566 mysql> CREATE ...
- 对Promise的研究4
Promise.reject() Promise.reject(reason)方法也会返回一个新的 Promise 实例,该实例的状态为rejected. const p = Promise.reje ...
- Change the environment variable for python code running
python程序运行中改变环境变量: Trying to change the way the loader works for a running Python is very tricky; pr ...