HDU 4323 编辑距离DP
来源:互联网 发布:js日期选择器插件 编辑:程序博客网 时间:2024/06/05 10:55
Magic Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1333 Accepted Submission(s): 562
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).
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.
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
15 265667931311783887 19509 1
Sample Output
Case #1:10
思路:这题一看肯定是编辑距离了,以前学长教过我们方法和算法,但是那时没有学DP,所以学长把把复杂化了,刚才研究了好久,把它改成了DP的,比较简便……不过刚才小地方弄了好久,弄了2个小时了才得过,差点晕死……
#include<iostream>#include<algorithm>#include<cstring>#include<cmath>#include<cstdio>using namespace std;const int M=1510;int b[M][M];char p[M][11],q[M][11];int DP(char *x,char *y){ int i,j,m,n,t;m=strlen(x);n=strlen(y); for(i=0;i<=(m>n?m:n);i++) { b[i][0]=i; b[0][i]=i; } for(i=1;i<=m;i++) for(j=1;j<=n;j++) if(x[i-1]==y[j-1]) b[i][j]=b[i-1][j-1]; //如果当前两个字母相等的话,编辑为0,这时就等于前一组的值了 else { b[i][j]=min(b[i-1][j]+1,b[i][j-1]+1); b[i][j]=min(b[i][j],b[i-1][j-1]+1); } return b[m][n];}int main(){int t,k=1;scanf("%d",&t);while(t--){int n,m,i,j,th[1505],l1,l2,sum;scanf("%d%d",&n,&m);for(i=0;i<n;i++)scanf("%s",p[i]);for(i=0;i<m;i++) scanf("%s%d",q[i],&th[i]); printf("Case #%d:\n",k++);for(i=0;i<m;i++){ sum=0; l1=strlen(q[i]);for(j=0;j<n;j++){ l2=strlen(p[j]); if(abs(l1-l2)>th[i]) continue; else if(DP(p[j],q[i])<=th[i]) sum++;}printf("%d\n",sum);}}return 0;}
int DP(char *x,char *y){ int i,j,m,n,t;m=strlen(x);n=strlen(y);for(i=0;i<m+1;i++) b[i][0]=i; for(j=0;j<n+1;j++) b[0][j]=j; for(i=1; i<m+1; i++) for(j=1; j<n+1; j++) { t=0; int min=1010; int c[3]={0}; if(x[i-1]==y[j-1]) t=0; else t=1; c[0]=b[i-1][j]+1; c[1]=b[i][j-1]+1; c[2]=b[i-1][j-1]+t; for(int k=0; k<3; k++) if(min>c[k])min=c[k]; b[i][j]=min; } return b[m][n];}
- HDU 4323 编辑距离DP
- HDU 4323 编辑距离DP
- hdu 4323 Magic Number (dp,编辑距离)
- hdu 4323 编辑距离
- HDU 4323 Magic Number 编辑距离(经典DP)
- HDU 4323 Magic Number(编辑距离 DP)
- hdu(4323)HDU 4323 Magic Number 编辑距离(经典DP)
- 编辑距离 (dp)
- 【dp】编辑距离问题
- zjut1699-编辑距离dp
- 编辑距离问题 dp
- DP解编辑距离
- 编辑距离(DP)
- dp-编辑距离HLG
- dp 编辑距离问题
- 1183 编辑距离 dp
- dp - 编辑距离
- DP-编辑距离问题
- TCL命令学习
- CString/string/char *
- 条款20:协助完成"返回值优化(RVO)"
- OpenGL绘制直线和圆(中点法)
- char* CString string区别
- HDU 4323 编辑距离DP
- 利用create table t2 as select * from t1 where 1=0;来仅复制表结构不复制其数据
- Scanner的next与nextLine方法
- java存储图片到数据库
- 深入理解子类和父类之间关系
- 28,UC(07)
- Shell编程笔试题
- WF4.0 基础篇 (九) 控制流Activity
- vc++2005各种数值转换总结