[Algorithm] similar string

Source: Internet
Author: User
ArticleDirectory
    • Description
    • Input
    • Output
    • Data range

This is the first question of the Microsoft programming contest 2013. The question is as follows:

Description

For two strings of the same length, we define the distance as the number of characters corresponding to different positions. At the same time, we think that the closer the distance is, the closer the string is. For example, the distance between "0123" and "0000" is 3, and the distance between "0123" and "0213" is 2, so compared with "0000, "0213" is the most similar to "0123.

Now we can specify two strings S1 and S2. The length of S2 is not greater than that of S1. Search for a substring of the same length as S2 in S1 to minimize the distance.

Input

The input includes multiple groups of data. The first line is an integer T, indicating the number of groups of test data. Each group of test data occupies exactly two rows. The first row is S1 and the second row is S2. All strings only contain characters ranging from "0" to "9.

Output

For each group of test data, a single line of "case # C: D" is output ". Where, C indicates the number of the test data (starting from 1), and D indicates the minimum distance of the substring found.

Data range

1 ≤ t ≤100

Small Data: The length of a string cannot exceed 1000

Big Data: The length of a string cannot exceed 50000

 

Sample Input
3012345678932101020304050607080940420121221211
Sample output
Case #1: 2 case #2: 1 case #3: 1



The brute force solution to this question is easy to think.AlgorithmIs to compare each length of S2 and S1 with its equivalent sub-string, the time responsibility should be O (LEN (S1) * Len (S2 )), but obviously it should have better algorithms.

 Import  Java. util. vendor;  Public   Class  Simstr {  Public   Static   Void  Main (string [] ARGs) {system. setin (FCM); scanner s =New  Using (system. In );  Int T = S. nextint (); S. nextline ();  For ( Int T = 0; t <t; t ++ ) {System. Out. Print ( "Case #" + (t + 1) + ":" ); String S1 = S. nextline (); string S2 = S. nextline ();  Int M = S1.length (); Int N = S2.length ();  Char [] S1chars = S1.tochararray ();  Char [] S2chars = S2.tochararray ();  Int [] [] Nums = New   Int [10] [1000 ];  Int [] Ans = New   Int [1000];  For ( Int I = 0; I <m; I ++ ){  Int Dig = Integer. parseint (string. valueof (s1chars [I]); Nums [dig] [ 0] ++ ; Nums [dig] [Nums [dig] [ 0] = I ;}  For ( Int I = 0; I <n; I ++ ){  Int Dig =Integer. parseint (string. valueof (s2chars [I]);  For ( Int J = 1; j <= Nums [dig] [0]; j ++ ){  If (Nums [dig] [J]-I> = 0 ) {Ans [Nums [dig] [J] -I] ++ ;}}}  Int Max = 0 ;  For ( Int I = 0; I <m; I ++){  If (ANS [I]> Max) {max = Ans [I] ;}} system. Out. println (n - Max) ;}s. Close ();}} 

This algorithm is obviously better than the brute force method, and there should be a faster method.

 

 

 

 

 

 

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.