Hdu4323-magic Number (Levenshtein distance-edit distance)

Source: Internet
Author: User


There is many magic numbers whose lengths is less than 10. Given Some queries, each contains a single number, if the Levenshtein distance (see below) between the number in the query And a magic number is no. than a threshold, we call the magic number is the lucky number for that query. Could you find out how many luck numbers is there for each query?

Levenshtein Distance(from Wikipedia http://en.wikipedia.org/wiki/Levenshtein_distance):

In information theory and computer science, the Levenshtein distance are a string metric for measuring the amount of differ ence between, sequences.

The term edit distance are often used to refer specifically to Levenshtein distance.

The Levenshtein distance between and strings is defined as the minimum number of edits needed to transform one string into The other, with the allowable edit operations being insertion, deletion, or substitution of a single character. It is the named after Vladimir Levenshtein, who considered the distance in 1965.

For example, the Levenshtein distance between "kitten" and "sitting" are 3, since the following three edits change one into The other, and there is no-to-do it with fewer than three edits:

1.kitten→sitten (substitution of ' s ' for ' K ')
2.sitten→sittin (Substitution of ' I ' for ' e ')
3.sittin→sitting (insertion of ' G ' at the end).

There is several test cases. The first line contains a single number t shows that there is t cases. For each test case, there was 2 numbers in the first line:n (n <=) m (M <= +) where n is the number of Magi C numbers and M is the number of queries.

The next n lines, each line has a magic number. You can assume this each magic number is distinctive.

The next m lines, each of which has a query and a threshold. The length of each query are no more than and the threshold are no more than 3.

For each test case, the first line was "case #id:", where ID was the case number. Then output m lines. There is a number shows the answer of the corresponding query.


Here is a reference to Levenshtein distance, special to Wikipedia. In information theory and computer science, the Levenshtein distance are a string metric for measuring the Differe nCE between, sequences. Levenshtein distance between-words is the minimum number of single-character edits (i.e. insertions, deletions or subs Titutions) required to changing one word into the other. This is the minimum number of steps required to add, delete, and change a string to another string.

Levenshtein distance has an existing algorithm:

Mathematically, the Levenshtein distance between, strings (of length and respectively) is given by where


Where is the indicator function equal to 0, and equal to 1 otherwise.

First describe the principle of the algorithm:

    • If we can use the K operand to convert s[1...i] to t[1...j-1], we just need to add t[j] to the last side to convert s[1...i] to T[1...J] and the operand to k+1.
    • If we can use the K operand to convert s[1...i-1] to T[1...J], we just need to remove the s[i] from the last to complete the conversion, the operand is k+1.
    • If we can use the K operand to convert s[1...i-1] to t[1...j-1], we just need to replace s[i] with T[J] only if needed (s[i]! = T[j]), and the required operand is k+cost (cost represents whether conversion is required, if S[I]==T[J] , the cost is 0, otherwise 1).

For a clearer understanding, we use a two-dimensional table to understand:

0 1 2 3 4 5 6
B 1
A 2
T 3
Y 4
U 5

Initially, the first row and the first column are initialized to 0-n, which means that the insertion operation is inserted sequentially from the empty string to get the current string.

0 1 2 3 4 5 6
B 1 0
A 2
T 3
Y 4
U 5

The value of dp[1][1] is determined by the values left, above, and to the front. From the left, it means that the beauty from the empty to the Batyu B, only need one insertion operation, the operand and the same, from the top and from the left to resemble, the operand is a +, from the upper left, because the beauty B and Batyu B is equal, so do not need to operate, The operand is 0+0. Select the minimum 0+0 bit DP value.

0 1 2 3 4 5 6
B 1 0 1
A 2 1
T 3
Y 4
U 5

Similarly, we can fill out other values.

According to the principle of the algorithm, this problem can be solved. This problem asks for a whole bunch of editing distances with a given string that is less than or equal to the number of given threshold (thresholds).

#include <stdio.h>#include<string.h>#include<iostream>#include<stdlib.h>#include<math.h>using namespacestd;#defineN 15#defineM 1505intMIN (intAintBintc) {    if(b<a) a=b; if(c<a) a=C; returnA;}intMain () {intT,tc=1, Count,magic_num,query_num,threshold,dp[n][n],cost; CharMagic[m][n],query[n]; scanf ("%d",&T);  while(tc<=T) {scanf ("%d%d",&magic_num,&query_num);  for(intI=0; i<magic_num;i++) scanf ("%s", Magic[i]);  for(intI=0; i<n;i++) {dp[0][i]=i; dp[i][0]=i; } printf ("Case #%d:\n", TC);  while(query_num--) {scanf ("%s%d",&query,&threshold); Count=0;  for(intI=0; i<magic_num;i++ ){                 for(intj=1; J<=strlen (Magic[i]); J + + ){                     for(intk=1; K<=strlen (query); k++ ){                        if(magic[i][j-1]==query[k-1]) cost=0; Else Cost=1; DP[J][K]=min (dp[j-1][k]+1, dp[j][k-1]+1, dp[j-1][k-1]+Cost ); }                }                if(Dp[strlen (Magic[i])][strlen (query)]<=threshold) Count++; } printf ("%d\n", Count); } TC++; } System ("Pause"); return 0;}

Hdu4323-magic Number (Levenshtein distance-edit distance)

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.