Codeforces Round #394 (Div. 2) C. Dasha and Password

C. Dasha and Password
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

After overcoming the stairs Dasha came to classes. She needed to write a password to begin her classes. The password is a string of length n which satisfies the following requirements:

  • There is at least one digit in the string,
  • There is at least one lowercase (small) letter of the Latin alphabet in the string,
  • There is at least one of three listed symbols in the string: '#', '*', '&'.

Considering that these are programming classes it is not easy to write the password.

For each character of the password we have a fixed string of length m, on each of these n strings there is a pointer on some character. Thei-th character displayed on the screen is the pointed character in the i-th string. Initially, all pointers are on characters with indexes 1 in the corresponding strings (all positions are numbered starting from one).

During one operation Dasha can move a pointer in one string one character to the left or to the right. Strings are cyclic, it means that when we move the pointer which is on the character with index 1 to the left, it moves to the character with the index m, and when we move it to the right from the position m it moves to the position 1.

You need to determine the minimum number of operations necessary to make the string displayed on the screen a valid password.

Input

The first line contains two integers nm (3 ≤ n ≤ 50, 1 ≤ m ≤ 50) — the length of the password and the length of strings which are assigned to password symbols.

Each of the next n lines contains the string which is assigned to the i-th symbol of the password string. Its length is m, it consists of digits, lowercase English letters, and characters '#', '*' or '&'.

You have such input data that you can always get a valid password.

Output

Print one integer — the minimum number of operations which is necessary to make the string, which is displayed on the screen, a valid password.

Examples
input
3 4
1**2
a3*0
c4**
output
1
input
5 5
#*&#*
*a1c&
&q2w*
#a3c#
*&#*&
output
3
Note

In the first test it is necessary to move the pointer of the third string to one left to get the optimal answer.

In the second test one of possible algorithms will be:

  • to move the pointer of the second symbol once to the right.
  • to move the pointer of the third symbol twice to the right.

刚开始题意看错了,气啊,写了半天的bfs,宛如一个智障,

后来发现挺水的,保存每一个行这三个类型字符最近的距离,然后三层for循环组合找最小的

然而。。。还是wa了。。。。。。。。。。。。。。

找了半天也没找出错误,后来才发现定义的inf是0x3f3f3f3f,组合的时候三个数加起来,有可能会出现超int的情况。。。。

n和m都很小,inf不用定义很大

#include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#define ll long long  
#define inf 10000
using namespace std; 
char s[100][100];
int judge(int i,int j)
{
	if(s[i][j]>='0'&&s[i][j]<='9')
	{
		return 1;
	}
	if(s[i][j]>='a'&&s[i][j]<='z')
	return 2;
	if(s[i][j]=='#'||s[i][j]=='*'||s[i][j]=='&')
	return 0;
}
int main()
{
	int n,m,ans=inf,i,j,k,t1,t2,t3,t;
	int jvli[100][5];
	scanf("%d %d",&n,&m);
	for(i=0;i<100;i++)
	{
		for(j=0;j<4;j++)
		jvli[i][j]=inf;
	}
	for(i=0;i<n;i++)
	scanf("%s",s[i]);
	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
		{
			t=min(j,m-j);
			jvli[i][judge(i,j)]=min(jvli[i][judge(i,j)],t);
		}
	}
	
	for(i=0;i<n;i++)
	{
		for(j=i+1;j<n;j++)
		{
			for(k=j+1;k<n;k++)
			{
				t1=min(jvli[i][1]+jvli[j][2]+jvli[k][0],jvli[i][0]+jvli[j][1]+jvli[k][2]);
				t2=min(jvli[i][1]+jvli[j][0]+jvli[k][2],jvli[i][0]+jvli[j][2]+jvli[k][1]);
				t3=min(jvli[i][2]+jvli[j][1]+jvli[k][0],jvli[i][2]+jvli[j][0]+jvli[k][1]);
				t=min(t1,min(t2,t3));
				ans=min(ans,t);
			}
		}
	}
	printf("%d\n",ans);
 } 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值