hdu4323magic number【编辑距离dp】
2016-03-27 19:03
281 查看
[align=left]Problem Description[/align]
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).
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
1
5 2
656
67
9313
1178
38
87 1
9509 1
[align=left]Sample Output[/align]
Case #1:
1
0
[align=left]Author[/align]
BJTU
[align=left]Source[/align]
2012 Multi-University Training Contest 3
水到我不想解释了==绝壁是下午脑残了
/************
hdu4323
2016.3.27
530MS 1584K 1270 B G++
************/
#include <iostream>
#include<cstdio>
#include<string.h>
using namespace std;
char str[1509][12],st[12];
int dp[12][12];
int t,n,m,num;
int min(int x,int y){if(x<y)return x;return y;}
int max(int x,int y){if(x>y)return x;return y;}
int main()
{
//freopen("cin.txt","r",stdin);
int cas=1;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
printf("Case #%d:\n",cas++);
for(int i=0;i<n;i++)scanf("%s",str[i]+1);
while(m--)
{
scanf("%s%d",st+1,&num);
int sum=0;
int len1=strlen(st+1);
for(int i=0;i<n;i++)
{
int len2=strlen(str[i]+1);
//memset(dp,0x3f3f3f3f,sizeof(dp));
for(int j=0;j<=len1;j++) dp[j][0]=j;
for(int j=0;j<=len2;j++) dp[0][j]=j;
for(int j=1;j<=len1;j++)
for(int k=1;k<=len2;k++)
{
dp[j][k]=min(dp[j-1][k]+1,1+dp[j][k-1]);
dp[j][k]=min(dp[j][k],dp[j-1][k-1]+(str[i][k]==st[j]?0:1));
}
if(dp[len1][len2]<=num)sum++;
}
printf("%d\n",sum);
}
}
return 0;
}
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).
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
1
5 2
656
67
9313
1178
38
87 1
9509 1
[align=left]Sample Output[/align]
Case #1:
1
0
[align=left]Author[/align]
BJTU
[align=left]Source[/align]
2012 Multi-University Training Contest 3
水到我不想解释了==绝壁是下午脑残了
/************
hdu4323
2016.3.27
530MS 1584K 1270 B G++
************/
#include <iostream>
#include<cstdio>
#include<string.h>
using namespace std;
char str[1509][12],st[12];
int dp[12][12];
int t,n,m,num;
int min(int x,int y){if(x<y)return x;return y;}
int max(int x,int y){if(x>y)return x;return y;}
int main()
{
//freopen("cin.txt","r",stdin);
int cas=1;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
printf("Case #%d:\n",cas++);
for(int i=0;i<n;i++)scanf("%s",str[i]+1);
while(m--)
{
scanf("%s%d",st+1,&num);
int sum=0;
int len1=strlen(st+1);
for(int i=0;i<n;i++)
{
int len2=strlen(str[i]+1);
//memset(dp,0x3f3f3f3f,sizeof(dp));
for(int j=0;j<=len1;j++) dp[j][0]=j;
for(int j=0;j<=len2;j++) dp[0][j]=j;
for(int j=1;j<=len1;j++)
for(int k=1;k<=len2;k++)
{
dp[j][k]=min(dp[j-1][k]+1,1+dp[j][k-1]);
dp[j][k]=min(dp[j][k],dp[j-1][k-1]+(str[i][k]==st[j]?0:1));
}
if(dp[len1][len2]<=num)sum++;
}
printf("%d\n",sum);
}
}
return 0;
}
相关文章推荐
- 1011. A+B和C (15)
- LCD屏参数及应用举例
- Keil 保护视力背景颜色设置
- matlab 轻松绘制出曲线的动画效果
- selenium+phantomJS爬虫,适用于登陆限制强,点触验证码等一些场景
- 物联网MQTT协议分析和开源Mosquitto部署验证
- PHP中Trait特性
- 如何优雅的在 Microsoft word中插入代码
- Leetcode数组类题目
- Activity的生命周期
- 数值的整数次方
- Android-GridView的使用
- 62. Unique Paths
- MATLAB读入视频出错的解决方法
- matlab读取xml文件
- 第四周学习进度情况
- Java集合框架:HashMap
- WCF 一步一步 发布 WCF服务 到 IIS (图)
- java中this的用法
- POJ1703 Find them, Catch them(并查集)