A. Phone Code

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarpus has n friends in Tarasov city. Polycarpus knows phone numbers of all his friends: they are strings s1, s2, ..., sn. All these strings consist only of digits and have the same length.

Once Polycarpus needed to figure out Tarasov city phone code. He assumed that the phone code of the city is the longest common prefix of all phone numbers of his friends. In other words, it is the longest string c which is a prefix (the beginning) of each si for all i (1 ≤ i ≤ n). Help Polycarpus determine the length of the city phone code.

Input

The first line of the input contains an integer n (2 ≤ n ≤ 3·104) — the number of Polycarpus's friends. The following n lines contain strings s1, s2, ..., sn — the phone numbers of Polycarpus's friends. It is guaranteed that all strings consist only of digits and have the same length from 1 to 20, inclusive. It is also guaranteed that all strings are different.

Output

Print the number of digits in the city phone code.

Sample test(s)
input
4
00209
00219
00999
00909
output
2
input
2
1
2
output
0
input
3
77012345678999999999
77012345678901234567
77012345678998765432
output
12
Note

prefix of string t is a string that is obtained by deleting zero or more digits from the end of string t. For example, string "00209" has 6 prefixes: "" (an empty prefix), "0", "00", "002", "0020", "00209".

In the first sample the city phone code is string "00".

In the second sample the city phone code is an empty string.

In the third sample the city phone code is string "770123456789".


解题说明:此题的意思是求一组字符串的最长公共前缀,题目中说明用暴力的算法来做。只需要每次取第一个字符串的前pos个字符构成一个新的前缀字符串,然后循环即可。这里用到strncmp函数,目的是比较两个字符串的前n个字符。


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;

char a[30001][21];
int main() 
{
	int n,i;
	int max;
	int k,pos;
	char c[21];
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		scanf("%s",&a[i]);
	}
	max=0;
	pos=1;
	while(1)
	{
		k=0;
		for(i=0;i<pos;i++)
		{
			c[k++]=a[0][i];
		}
		c[k]='\0';

		for(i=1;i<n;i++)
		{
			if(strncmp(c,a[i],pos)!=0)
			{
				break;
			}
		}
		if(i==n)
		{
			max=pos;
			pos++;
		}
		else
		{
			break;
		}
	}
	printf("%d\n",max);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值