A. phone code

Source: Internet
Author: User
Time limit per test

2 seconds

Memory limit per test

256 megabytes

Input

Standard Input

Output

Standard output

Polycarpus hasNFriends in Tarasov city. polycarpus knows phone numbers of all his friends: they are stringsS1, bytes,S2, middle..., middle ,...,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 stringCWhich
Is a prefix (the beginning) of eachSIFor
AllI(1 digit ≤ DigitILimit ≤ limitN).
Help polycarpus determine the length of the city phone code.

Input

The first line of the input contains an integerN(2 cores ≤ CoresNLimit ≤ limit 3 · 104)
-The number of polycarpus's friends. The followingNLines contain stringsS1, bytes,S2, middle..., middle ,...,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,
Specified Sive. It is also guaranteed that all strings are different.

Output

Print the number of digits in the city phone code.

Sample test (s) Input
400209002190099900909
Output
2
Input
212
Output
0
Input
3770123456789999999997701234567890123456777012345678998765432
Output
12
Note

A prefix of stringTIs a string that is obtained by deleting zero or more
Digits from the end of stringT. 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 ".

Description: This question refers to finding the longest public prefix of a group of strings. The question describes the use of brute force algorithms. You only need to take the first POS character of the first string each time to form a new prefix string, and then loop. The strncmp function is used here to compare the first n characters of two strings.

#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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.