Codeforces #410(div2) B. Mike and strings (暴力枚举
来源:互联网 发布:量子 知乎 编辑:程序博客网 时间:2024/06/10 02:26
B. Mike and strings
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?
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.
Print the minimal number of moves Mike needs in order to make all the strings equal or print - 1 if there is no solution.
4
xzzwo
zwoxz
zzwox
xzzwo
5
2
molzv
lzvmo
2
3
kc
kc
kc
0
3
aa
aa
ab
-1
In the first sample testcase the optimal scenario is to perform operations in such a way as to transform all strings into “zwoxz“.
暴力枚举所有情况QAQ
#include <cstdio>#include <cstring>#include <cmath>#include <queue>#include <stack>#include <map>#include <set>#include <iostream>#include <vector>#include <algorithm>using namespace std;#define ll long long#define inf 0x3f3f3f3fconst int mod = 1e9+7;const int N = 1010;char q[100], a[100],w[100];char str[N][N];int n, len;void f(char aa[],char b[], int x){ for(int i = 0;i < len; i++) { aa[i] = b[(i+x)%len]; }// aa[len] = '\0';}int main(){ scanf("%d",&n); getchar(); scanf("%s",a); for(int i = 1;i < n; i++) scanf("%s",str[i]); len = strlen(a); int ans = inf; for(int i = 0;i <= len-1; i++) { int tmp = i; f(q,a,i); int go = 0; for(int j = 1; j < n; j++) { int low = inf; for(int k = 0;k <= len-1; k++) { f(w,str[j],k); if(strcmp(q,w)==0) { tmp += k; go++; break; } } } if(go == n-1) ans = min(ans,tmp); } if(ans == inf) puts("-1"); else printf("%d\n",ans);return 0;}
- Codeforces #410(div2) B. Mike and strings (暴力枚举
- Mike and strings-codeforces-Round 410-Div2-B
- codeforces B. Mike and strings 暴力+字符串
- codeforces B. Mike and strings
- Codeforces 798B Mike and strings
- codeforces 798B Mike and strings
- CodeForces 798B Mike and strings
- B. Mike and strings
- B. Mike and strings
- B. Mike and strings
- B. Mike and strings
- Codefroces 410 B.Mike and strings
- 小白题解 Codeforces 798B Mike and strings
- Codeforces Round #410 (Div. 2) B. Mike and strings(字符串匹配)
- Codeforces Round #410 (Div. 2)-B. Mike and strings-string的使用和取模
- Codeforces Round #410 (Div. 2)-B. Mike and strings【STL+略贪心】
- Mike and gcd problem-codeforces-Round 410 Div2-C
- Codeforces Round #410 Div2 D Mike and distribution
- nginx+tomcat集群
- java 十进制转二进制和十六进制
- Spark源码系列(九)Spark SQL初体验之解析过程详解
- 如何利用Element元素在末尾添加节点
- python3.6连接MySQL
- Codeforces #410(div2) B. Mike and strings (暴力枚举
- 积分系统(3)-详细公告页面之Controller层
- [BZOJ3451][Tyvj1953]Normal
- Captcha Cracker
- Leetcode-标签为Tree 543. Diameter of Binary Tree
- LeetCode#160.Intersection of Two Linked Lists
- innerHTML使用入门
- 关于JDBC中的Mysql驱动所遇到的坑
- 逆向工程核心原理学习笔记(四):检索API方法2-设置断点