CodeForces 798B Mike and strings
来源:互联网 发布:httpclient工具类json 编辑:程序博客网 时间:2024/05/21 07:59
题目链接:http://codeforces.com/contest/798/problem/B
题意:给你n个字符串,有一种操作叫做左移,左移一次就把第一个字符加到这个字符串的末尾,问你最少的操作数使得,这个n个字符串都相等,若不存在这种操作就输出-1
解析:由于n和字符串的长度都不大,那么直接暴力搞就好,枚举最终的字符串的答案,然后逐个匹配,然后更新ans记录最小值即可
#include <bits/stdc++.h>using namespace std;const int inf = 0x7fffffff;string slove(string s,int k,int len){ return s.substr(k,len-k)+s.substr(0,k);}int main(void){ int n; scanf("%d",&n); string a[55]; for(int i=0;i<n;i++) cin>>a[i]; int flag = 1,ans = inf; string t1 = a[0]; int len = t1.length(); for(int k=0;k<len;k++) { string tmp = slove(t1,k,len); //cout<<k<<" "<<tmp<<endl; int sum = 0; for(int i=0;i<n;i++) { int j=0; string tt; for(;j<len;j++) { tt = slove(a[i],j,len); if(tt==tmp) { sum += j; break; } } if(j==len) { flag = 0; break; } } if(!flag) break; ans = min(ans,sum); } if(!flag) puts("-1"); else printf("%d\n",ans); return 0;}
0 0
- 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
- codeforces B. Mike and strings 暴力+字符串
- B. Mike and strings
- B. Mike and strings
- B. Mike and strings
- B. Mike and strings
- Mike and strings-codeforces-Round 410-Div2-B
- Codeforces #410(div2) B. Mike and strings (暴力枚举
- CF#798 B. Mike and strings(思维)
- 【CodeFores 798B】 Mike and strings(模拟+string)
- Codefroces 410 B.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+略贪心】
- zb的生日(dfs)
- 百炼4105:拯救公主
- #POJ2516#Minimum Cost(拆点+Km完备匹配)
- Task failed to report status for 1200 seconds. Killing!老碰到这种问题怎么解
- 一起学ORBSLAM2(1)跑通ORBSLAM2 ubuntu 14.04的运行
- CodeForces 798B Mike and strings
- D
- 三层登录实现 C#版
- 注解:SpringMVC中表单参数的限定和获取表单参数以及获取request,response,session+方法返回值讲解
- 设计模式之单例模式
- 吝啬的国度(dfs)
- C++做题总结(1)
- js代码片段
- 多校联合集训 A. 字符串“水”题 (状压+哈希)