CF - 798B. Mike and strings - 字符串+暴力

1.题目描述:

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
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".


2.题意概述:

给你n个字符串,你对于其中某个串有个操作——把串第一个字符放到最后一位......问你能否通过这样的操作使得所有字符串相同?如果能则输出最小操作数

3.解题思路:

一看n才50,直接暴力,先检查字符是否相同,再暴力以每个字符串为模板去找其他字符串变成相应字符串需要多少操作数,更新最小值。

4.AC代码:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 100100
#define lson root << 1
#define rson root << 1 | 1
#define lent (t[root].r - t[root].l + 1)
#define lenl (t[lson].r - t[lson].l + 1)
#define lenr (t[rson].r - t[rson].l + 1)
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
int vis[51][26];
string ch[51];
string tmp;
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	long _begin_time = clock();
#endif
	int n;
	while (~scanf("%d", &n))
	{
		int len = 0;
		for (int i = 0; i < n; i++)
		{
			cin >> ch[i];
			if (i == 0)
				len = ch[i].length();
		}
		memset(vis, 0, sizeof(vis));
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < len; j++)
				vis[i][ch[i][j] - 'a']++;
		}
		bool flag = 1;
		for (int i = 0; i < 26 && flag; i++)
		{
			for (int j = 1; j < n; j++)
				if (vis[j - 1][i] != vis[j][i])
				{
					flag = 0;
					break;
				}
		}
		if (!flag)
		{
			puts("-1");
			continue;
		}
		int ans = INF;
		for (int i = 0; i < n && flag; i++)
		{
			int cnt = 0;
			for (int j = 0; j < n; j++)
			{
				if (i != j)
				{
					int pos = 0, flag1 = 0;
					while (pos < len)
					{
						tmp = ch[j];
						string tmp1 = tmp.substr(0, pos);
						tmp.erase(0, pos);
						tmp.append(tmp1);
						if (tmp == ch[i])
						{
							flag1 = 1;
							break;
						}
						pos++;
					}
					if (flag1)
						cnt += pos;
					else
						flag = 0;
				}
			}
			if (flag)
				ans = min(ans, cnt);
		}
		if (flag)
			printf("%d\n", ans);
		else
			puts("-1");
	}
#ifndef ONLINE_JUDGE
	long _end_time = clock();
	printf("time = %ld ms.", _end_time - _begin_time);
#endif
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值