Codefroces 410 B.Mike and strings
来源:互联网 发布:windows仿mac软件 编辑:程序博客网 时间:2024/06/13 02:37
http://codeforces.com/problemset/problem/798/B
题目大意:给出n个字符串,可以把每个字符串的第一个字符加到字符串最后。问全部相同的话需要的最少操作次数是多少。
解法:因为只有50个字符串,字符串最大长度为50,直接枚举。复杂度O(n^3)
代码如下:
#include<bits/stdc++.h>using namespace std;int main(){int n, cnt, ans = 0x3f3f3f3f;string str[55], goal;bool flag = 1;scanf("%d", &n);for(int i = 0; i < n; i++)cin >> str[i];goal = str[0];for(int i = 0; i < goal.size() && flag; i++){char ch = goal[0];goal.erase(goal.begin());goal += ch;cnt = 0;for(int j = 0; j < n && flag; j++){flag = 0;for(int k = 0; k < str[j].size(); k++){if(!flag && str[j] == goal){flag = 1;cnt += k;}char t = str[j][0];str[j].erase(str[j].begin());str[j] += t;}}if(ans > cnt)ans = cnt;}if(!flag)cout << "-1" << endl;elsecout << ans << endl;return 0;}
0 0
- Codefroces 410 B.Mike and strings
- B. Mike and strings
- B. Mike and strings
- B. Mike and strings
- B. Mike and strings
- codeforces B. Mike and strings
- Codefroces 410 A. Mike and palindrome
- Codefroces 410 C. Mike and gcd problem
- Mike and strings-codeforces-Round 410-Div2-B
- Codeforces #410(div2) B. Mike and strings (暴力枚举
- Codeforces 798B Mike and strings
- codeforces 798B Mike and strings
- codeforces B. Mike and strings 暴力+字符串
- CodeForces 798B Mike and strings
- Codeforces798B-Mike and strings
- Codeforces798B Mike and strings
- Codeforces Round #410 (Div. 2) B. Mike and strings(字符串匹配)
- Codeforces Round #410 (Div. 2)-B. Mike and strings-string的使用和取模
- 第八届蓝桥杯纸牌三角形
- 计算机牛人资料大全
- ORACLE PLSQL代码块学习
- 【日常】凸轮大法好
- Java设计模式《十七》单例模式
- Codefroces 410 B.Mike and strings
- binary-tree-zigzag-level-order-traversal
- 网易互联网2017在线笔试题目-魔力手环
- 插入排序——希尔排序
- Bash-shell
- Java设计模式《十八》桥接模式
- LeetCode 543. Diameter of Binary Tree
- bfs和dfs:poj2386和leetcode130
- linux 使用jrtplib收发h.264视频文件