【CodeFores 798B】 Mike and strings(模拟+string)
来源:互联网 发布:js 变量 undefined 编辑:程序博客网 时间:2024/05/17 21:41
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.
4xzzwozwoxzzzwoxxzzwo
5
2molzvlzvmo
2
3kckckc
0
3aaaaab
-1
In the first sample testcase the optimal scenario is to perform operations in such a way as to transform all strings into "zwoxz".
题目大意:给n个字符串,将第i个字符串移动xi位,使得最终所有字符串相等,求最小的移动步数和,若无法相等输出-1
思路:n最大50个,枚举每一个字符串作为基础串,求得每种情况的移动步数和,取最小值。string find大发好,比赛时忘了用这个,结果没怼出来。果然还是不太熟悉
#include <bits/stdc++.h>#define INF 0x3f3f3f3fusing namespace std;int main(){ int n,ans; string s,t,p; while(~scanf("%d",&n)){ vector<string>v; ans=INF; for (int i=0; i<n; i++){ cin>>s; v.push_back(s); } for (int i=0; i<n; i++){ int sum=0; t=v[i]; for (int j=0; j<n; j++){ if (i!=j){ p=v[j]; p+=p; int k=p.find(t); //返回下标 if (k==-1){ sum=-1; break; } sum+=k; } } if (sum != -1) ans=min(ans,sum); } if (ans==INF) printf("-1\n"); else printf("%d\n",ans); } return 0;}
- 【CodeFores 798B】 Mike and strings(模拟+string)
- B. Mike and strings
- B. Mike and strings
- B. Mike and strings
- B. Mike and strings
- CF#798 B. Mike and strings(思维)
- Codeforces 798B Mike and strings
- codeforces 798B Mike and strings
- CodeForces 798B Mike and strings
- codeforces B. Mike and strings
- 小白题解 Codeforces 798B Mike and strings
- Codefroces 410 B.Mike and strings
- codeforces B. Mike and strings 暴力+字符串
- Codeforces Round #410 (Div. 2)-B. Mike and strings-string的使用和取模
- CodeForces - 548B Mike and Fun (模拟)
- CF798B:Mike and strings(Hash)
- Codeforces798B-Mike and strings
- Codeforces798B Mike and strings
- 复习笔记-java代码块
- lintcode(632)二叉树的最大节点
- 541. Reverse String II
- linux中的grep命令用法
- C# 调用opencore-amr1.5
- 【CodeFores 798B】 Mike and strings(模拟+string)
- 一周搜索热点20170503
- Junit单元测试
- Java编码标准
- hadoop搭建之HDFS,MapReduce
- iOS开发
- 帧动画的实现
- 构造(FZU 2111,Min Number)
- Sublime Text3配置在可交互环境下运行python快捷键