HDU4323(Magic Number)

Magic Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2362 Accepted Submission(s): 1011

Problem Description
There are many magic numbers whose lengths are 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 more than a threshold, we call the magic number is the lucky number for that query. Could you find out how many luck numbers are there for each query?

Levenshtein distance (from Wikipedia http://en.wikipedia.org/wiki/Levenshtein_distance):
In information theory and computer science, the Levenshtein distance is a string metric for measuring the amount of difference between two sequences. The term edit distance is often used to refer specifically to Levenshtein distance.
The Levenshtein distance between two 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 named after Vladimir Levenshtein, who considered this distance in 1965.
For example, the Levenshtein distance between “kitten” and “sitting” is 3, since the following three edits change one into the other, and there is no way 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).

Input
There are several test cases. The first line contains a single number T shows that there are T cases. For each test case, there are 2 numbers in the first line: n (n <= 1500) m (m <= 1000) where n is the number of magic numbers and m is the number of queries.
In the next n lines, each line has a magic number. You can assume that each magic number is distinctive.
In the next m lines, each line has a query and a threshold. The length of each query is no more than 10 and the threshold is no more than 3.

Output
For each test case, the first line is “Case #id:”, where id is the case number. Then output m lines. For each line, there is a number shows the answer of the corresponding query.

Sample Input
1
5 2
656
67
9313
1178
38
87 1
9509 1

Sample Output
Case #1:
1
0

题解:这是一道编辑距离(DP)的模板题目,个人感觉会LCS(最长公共子序列)就很简单了。

编辑距离,又称levenshtein距离,是指两个字符串之间,有一个转成另一个距离的最少操作次数。如果他们的距离越大,说明他们约不相同。许可的编辑操作包括一个字符替换成另一个字符,插入一个字符,删除一个字符。

DP题目一般有状态设计,状态转移方程,边界处理

状态转移方程

1.如果i=0,j=0,edit[i][j]=0;
2.如果i=0,j>0,edit[i][j]=j;
3.如果i>0,j=0,edit[i][j]=i
4.如果i>0,j>0,edit[i][j]=min(min(edit[i-1][j],edit[i][j-1])+1,edit[i-1][j-1]+flag)
----4.1如果a[i]=b[j],flag=0
----4.2如果a[i]!=b[j],flag=1;

摘自博客:https://blog.csdn.net/baodream/article/details/80417695
edit[i-1][j]+1相当于给word2的最后插入了word1的最后的字符,插入操作使得edit+1,之后计算edit[i-1][j];
edit[i][j-1]+1相当于将word2的最后字符删除,删除操作edit+1,之后计算edit[i][j-1];
edit[i-1][j-1]+flag相当于通过将word2的最后一个字符替换为word1的最后一个字符。flag标记替换的有效次数。

核心代码:

1.处理好i=0和j=0的边界
2.穷举利用状态转移方程打表

int edit(string s1,string s2){
	int len1=s1.size();
	int len2=s2.size();
	memset(dp,0,sizeof(dp));
	for(int i=0;i<=len1;i++)//边界处理 
		dp[i][0]=i;
	for(int j=0;j<=len2;j++)//边界处理 
		dp[0][j]=j;
	for(int i=1;i<=len1;i++){
		for(int j=1;j<=len2;j++){
			int flag=1;
			if(s1[i-1]==s2[j-1])
				flag=0;
			dp[i][j]=min(min(dp[i-1][j],dp[i][j-1])+1,dp[i-1][j-1]+flag);//状态转移方程 
		}
	}
	return dp[len1][len2];
}

AC代码:

#include<iostream>
#include<string>
#include<string.h>
#include<algorithm> 
#define INF 0x3f3f3f3f
using namespace std;
string s[1500];
int dp[15][15];
int edit(string s1,string s2){
	int len1=s1.size();
	int len2=s2.size();
	memset(dp,0,sizeof(dp));
	for(int i=0;i<=len1;i++)//边界处理 
		dp[i][0]=i;
	for(int j=0;j<=len2;j++)//边界处理 
		dp[0][j]=j;
	for(int i=1;i<=len1;i++){
		for(int j=1;j<=len2;j++){
			int flag=1;
			if(s1[i-1]==s2[j-1])
				flag=0;
			dp[i][j]=min(min(dp[i-1][j],dp[i][j-1])+1,dp[i-1][j-1]+flag);//状态转移方程 
		}
	}
	return dp[len1][len2];
}
int main(){
	int t,n,m,l,ans;
	string s1;
	cin>>t;
	for(int k=1;k<=t;k++){
		cin>>n>>m;
		for(int i=0;i<n;i++)
			cin>>s[i];
		cout<<"Case #"<<k<<":"<<endl;
		for(int i=0;i<m;i++){
			cin>>s1>>l;
			ans=0;
			for(int j=0;j<n;j++){
				if(edit(s[j],s1)<=l){
					ans++;
				}
			}
			cout<<ans<<endl;
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值