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;
}
最新文章
- ubuntu引导修复
- nodejs初探(一)环境搭建,开发工具安装
- hexdump—Linux系统的二进制文件查看工具
- UIView 周围出现黑线的解决方法
- C++实现python标准库中的Counter
- go语言使用redis —— redigo
- 【Quick 3.3】资源脚本加密及热更新(一)脚本加密
- c++学籍管理系统
- IOS开发创建开发证书及发布App应用(一)——流程说明
- GO 语言简介(网摘)
- python函数调用之自我调用与C++比较
- 测试APPEND INSERT是否产生UNDO信息的过程
- Docker 学习7 Dockerfile详解
- windows安装tf
- 使用nexus搭建maven私服教程详解
- 20165315 C语言学习情况与Java学习目标
- leetcode748
- rabbitmq系列一 之简单队列
- jQuery学习- 子选择器与可见性选择器
- IVF link error错误不显示的问题