HDU-4323Magic Number
来源:互联网 发布:成都网络公关公司 编辑:程序博客网 时间:2024/05/16 02:26
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.
一道最短编辑距离,题目数据不算很强,所以暴力的找了一遍也过了。似乎正规解法是用BK书。
Code:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
using namespace std;
char num[1505][15],s[15];
int dp[15][15],lenb,th,lena[1505];
int Edit(int x)
{
int i,j,Insert,Delete,Replace;
if(abs(lena[x]-lenb)>th) return 1e9;
for(i = 0;i<=lena[x];i++) dp[i][0]=i;
for(j = 0;j<=lenb;j++) dp[0][j]=j;
for(i = 1;i<=lena[x];i++)
for(j = 1;j<=lenb;j++)
{
Insert=dp[i][j-1]+1;
Delete=dp[i-1][j]+1;
if(num[x][i-1]==s[j-1]) Replace=dp[i-1][j-1];
else Replace=dp[i-1][j-1]+1;
dp[i][j]=min(min(Insert,Delete),Replace);
}
return dp[lena[x]][lenb];
}
int main()
{
int T,Case;
scanf("%d",&T);
for(Case=1;Case<=T;Case++)
{
int n,m,i,j,ans;
scanf("%d%d",&n,&m);
for(i = 0;i<n;i++)
scanf("%s",num[i]),lena[i]=strlen(num[i]);
printf("Case #%d:\n",Case);
for(i = 0;i < m;i++)
{
scanf("%s%d",s,&th);
lenb=strlen(s);ans=0;
for(j = 0;j<n;j++)
{
if(abs(lena[j]-lenb)<=th)
if(Edit(j)<=th) ans++;
}
printf("%d\n",ans);
}
}
}
#include<cstring>
#include<algorithm>
#include<math.h>
using namespace std;
char num[1505][15],s[15];
int dp[15][15],lenb,th,lena[1505];
int Edit(int x)
{
int i,j,Insert,Delete,Replace;
if(abs(lena[x]-lenb)>th) return 1e9;
for(i = 0;i<=lena[x];i++) dp[i][0]=i;
for(j = 0;j<=lenb;j++) dp[0][j]=j;
for(i = 1;i<=lena[x];i++)
for(j = 1;j<=lenb;j++)
{
Insert=dp[i][j-1]+1;
Delete=dp[i-1][j]+1;
if(num[x][i-1]==s[j-1]) Replace=dp[i-1][j-1];
else Replace=dp[i-1][j-1]+1;
dp[i][j]=min(min(Insert,Delete),Replace);
}
return dp[lena[x]][lenb];
}
int main()
{
int T,Case;
scanf("%d",&T);
for(Case=1;Case<=T;Case++)
{
int n,m,i,j,ans;
scanf("%d%d",&n,&m);
for(i = 0;i<n;i++)
scanf("%s",num[i]),lena[i]=strlen(num[i]);
printf("Case #%d:\n",Case);
for(i = 0;i < m;i++)
{
scanf("%s%d",s,&th);
lenb=strlen(s);ans=0;
for(j = 0;j<n;j++)
{
if(abs(lena[j]-lenb)<=th)
if(Edit(j)<=th) ans++;
}
printf("%d\n",ans);
}
}
}
阅读全文
0 0
- hdu 4323 Magic Number
- Hdu 4323 Magic Number
- HDU-4323Magic Number
- hdu 4323 Magic Number( DP )
- hdu - 4323 - Magic Number - dp + 数据结构优化
- hdu 4323 Magic Number(编辑距离)
- hdu 4323 Magic Number (dp,编辑距离)
- HDU 4323 Magic Number(DP)
- HDU 4323 Magic Number 编辑距离
- hdu 4323 Magic Number 编辑距离
- 【HDU】5796 Magic Number【lca】
- HDU 4323 Magic Number 编辑距离(经典DP)
- HDU 4323 Magic Number(编辑距离 DP)
- hdu 4323 Magic Number (最短编辑距离)
- hdu(4323)HDU 4323 Magic Number 编辑距离(经典DP)
- magic number
- magic number
- Magic number
- oracle rac报错:PRVF-9652 : Cluster Time Synchronization Services check failed
- Python 描述符代码示例
- mysql-mmm-monitor服务启动错误,没有编译相关的Class::Singleton包
- 利用代码下载Apk
- HTML学习1
- HDU-4323Magic Number
- sql数据库备份和恢复
- AbstractQueuedSynchronizer 之 共享锁
- aliyun E-MapReduce 使用指南
- FCC----------- Sum All Odd Fibonacci Numbers
- 51nod 1624-取余最长路(set+二分)
- 分段函数的输入,太啰嗦了。
- list排序-降序
- 丑数