CodeForces - 798B Mike and strings —— 暴力
2017-09-24 10:48
281 查看
B. Mike and strings
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Examples
input
output
input
output
input
output
input
output
Note
In the first sample testcase the optimal scenario is to perform operations in such a way as to transform all strings into "zwoxz".
题意:给定n个字符串 每次操作可以把某个串前移一位 即最前面的字符移到最后面 问要使得所有的串都一样 最少需要移动几次
思路:枚举每一个字符串当作目标串 计算其他串移动到该串的次数 取最小即可
用vector的erase和insert 实现的题意操作
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#define max_ 70#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
char tr[max_][max_];
vector<char>v1,v2;
int n;
int main(int argc, char const *argv[])
{
scanf("%d",&n);
int i,j,k;
for(i=1;i<=n;i++)
scanf(" %s",tr[i]);
int l=strlen(tr[1]);
int minn=inf;
for(i=1;i<=n;i++)
{
int cntsum=0;
for(j=1;j<=n;j++)
{
int cnt=0;
if(i!=j)
{
v1.clear();
v2.clear();
for(k=0;k<l;k++)
{
v1.push_back(tr[i][k]);
v2.push_back(tr[j][k]);
}
while(v1!=v2)
{
v2.insert(v2.end(),v2[0]);
v2.erase(v2.begin());
cnt++;
if(cnt>=l)
{
printf("-1\n");
return 0;
}
}
cntsum+=cnt;
}
}
if(cntsum<minn)
minn=cntsum;
}
printf("%d\n",minn );
return 0;
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Examples
input
4 xzzwo zwoxz zzwox xzzwo
output
5
input
2 molzv lzvmo
output
2
input
3 kc kc kc
output
0
input
3 aa aa ab
output
-1
Note
In the first sample testcase the optimal scenario is to perform operations in such a way as to transform all strings into "zwoxz".
题意:给定n个字符串 每次操作可以把某个串前移一位 即最前面的字符移到最后面 问要使得所有的串都一样 最少需要移动几次
思路:枚举每一个字符串当作目标串 计算其他串移动到该串的次数 取最小即可
用vector的erase和insert 实现的题意操作
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#define max_ 70#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
char tr[max_][max_];
vector<char>v1,v2;
int n;
int main(int argc, char const *argv[])
{
scanf("%d",&n);
int i,j,k;
for(i=1;i<=n;i++)
scanf(" %s",tr[i]);
int l=strlen(tr[1]);
int minn=inf;
for(i=1;i<=n;i++)
{
int cntsum=0;
for(j=1;j<=n;j++)
{
int cnt=0;
if(i!=j)
{
v1.clear();
v2.clear();
for(k=0;k<l;k++)
{
v1.push_back(tr[i][k]);
v2.push_back(tr[j][k]);
}
while(v1!=v2)
{
v2.insert(v2.end(),v2[0]);
v2.erase(v2.begin());
cnt++;
if(cnt>=l)
{
printf("-1\n");
return 0;
}
}
cntsum+=cnt;
}
}
if(cntsum<minn)
minn=cntsum;
}
printf("%d\n",minn );
return 0;
}
相关文章推荐
- Codeforces 673C Bear and Colors【暴力枚举】
- Codeforces 548C - Mike and Frog(暴力循环节)
- Codeforces 425A Sereja and Swaps(暴力枚举)
- Codeforces 868C - Qualification Rounds 【状态压缩+暴力枚举】
- codeforces 574B 暴力+复杂度分析
- Codeforces 466A Cheap Travel【水题】暴力
- Codeforces Round #439 (Div. 2) Problem E (Codeforces 869E) - 暴力 - 随机化 - 二维树状数组 - 差分
- CodeForces 474C Captain Marmot (数学,旋转,暴力)
- Codeforces 617C Watering Flowers 【暴力 数据范围】
- Codeforces 363B Fence-暴力
- 【CodeForces】716B - Complete the Word(暴力)
- Codeforces--629A--Far Relative’s Birthday Cake(暴力模拟)
- CodeForces 730G Car Repair Shop (暴力)
- codeforces 626D 概率+暴力
- CodeForces 29D - Ant on the Tree 暴力LCA
- CodeForces - 908B New Year and Buggy Bot (全排列+暴力)
- Codeforces 842A Kirill And The Game【暴力,水】
- Codeforces 400A Inna and Choose Options(暴力)
- codeforces 324# E. Anton and Ira (暴力枚举+贪心)
- Codeforces 665C Simple Strings【暴力,贪心】