hdu 1516 String Distance and Transform Process(编辑距离+记录路径)
来源:互联网 发布:打印机无法配置该端口 编辑:程序博客网 时间:2024/06/05 15:07
String Distance and Transform Process
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 486 Accepted Submission(s): 228
Special Judge
Problem Description
String Distance is a non-negative integer that measures the distance between two strings. Here we give the definition. A transform list is a list of string, where each string, except for the last one, can be changed to the string followed by adding a character, deleting a character or replacing a character. The length of a transform list is the count of strings minus 1 (that is the count of operations to transform these two strings). The distance between two strings is the length of a transform list from one string to the other with the minimal length. You are to write a program to calculate the distance between two strings and give the corresponding transform list.
Input
Input consists a sequence of string pairs, each string pair consists two lines, each string occupies one line. The length of each string will be no more than 80.
Output
For each string pair, you should give an integer to indicate the distance between them at the first line, and give a sequence of command to transform string 1 to string 2. Each command is a line lead by command count, then the command. A command must be
Insert pos,value
Delete pos
Replace pos,value
where pos is the position of the string and pos should be between 1 and the current length of the string (in Insert command, pos can be 1 greater than the length), and value is a character. Actually many command lists can satisfy the request, but only one of them is required.
Insert pos,value
Delete pos
Replace pos,value
where pos is the position of the string and pos should be between 1 and the current length of the string (in Insert command, pos can be 1 greater than the length), and value is a character. Actually many command lists can satisfy the request, but only one of them is required.
Sample Input
abcacbcdaaaaabaaaa
Sample Output
31 Delete 12 Replace 3,d3 Delete 441 Insert 1,a2 Insert 2,a3 Insert 3,b4 Insert 7,a
分析:构造一个二位数组,dp[i][j] 表示 长度为 i 的 A序列,变成长度为 j 的 B 序列要做的操作数。(注意 i j 可以为0 即 空序列)
代码如下:
#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int maxn = 85;char A[maxn],B[maxn];int dp[maxn][maxn],len1,len2;void path(){ int i,j,step; int index=1; i=len1; j=len2; step=dp[len1][len2]; while(i>0 || j>0) { //printf("step == %d \n",step); //printf("i,j %d %d\n",i,j); if(i>0 && j==0) { printf("%d Delete %d\n",index++,i); i--; continue; } else if(i==0 && j>0) { printf("%d Insert 1,%c\n",index++,B[j-1]); j--; continue; } else { if(step == dp[i-1][j-1] && A[i-1] == B[j-1]) { i--; j--; } else if(step == dp[i-1][j-1]+1) { printf("%d Replace %d,%c\n",index++,i,B[j-1]); step--; i--; j--; } else if(step == dp[i][j-1]+1) { printf("%d Insert %d,%c\n",index++,i+1,B[j-1]); step--; j--; } else if( step == dp[i-1][j]+1) { printf("%d Delete %d\n",index++,i); step--; i--; } } }}int main(){ while(scanf("%s %s",A,B)!=EOF) { getchar(); len1 = strlen(A); len2 = strlen(B); memset(dp,0,sizeof(dp)); for(int i = 0; i <= len1; i++)//初始化空序列的时候需要的操作数 dp[i][0] = i; for(int i = 0; i <= len2; i++) dp[0][i] = i; for(int i = 1; i <= len1; i++) { for(int j = 1; j <= len2; j++) { int tmp = min(dp[i][j-1],dp[i-1][j]) + 1;//有三种状态可以转化到当前的dp[i][j],其中第三种分情况,如果相同就省了一步操作 int d = A[i-1] == B[j-1] ? 0 : 1; dp[i][j] = min(tmp,dp[i-1][j-1]+d); } } /*for(int i=1;i<=len1;i++) { for(int j=1;j<=len2;j++) printf("%d ",dp[i][j]); printf("\n"); }*/ printf("%d\n",dp[len1][len2]); path(); } return 0;}
阅读全文
0 0
- hdu 1516 String Distance and Transform Process(编辑距离+记录路径)
- hdu-1516 String Distance and Transform Process(编辑距离+路径)
- hdu 1516 String Distance and Transform Process(编辑距离+保存修改路径)
- 杭电acm之1516--String Distance and Transform Process
- UVa 526 - String Distance and Transform Process
- hdu 1516(编辑距离+记录路径)
- zju/zoj 1459 String Distance and Transform Process
- 编辑距离(Levenshtein Distance)
- 编辑距离(edit distance)
- 编辑距离(Levenshtein Distance)
- Levenshtein Distance(编辑距离)
- 编辑距离(Edit Distance | Levenshtein距离)
- 编辑距离(Levenshtein Distance) (转)
- “最小编辑距离(Minimum Edit Distance)”
- 最小编辑距离(Minimum edit distance)
- LeetCode | Edit Distance(字符串编辑距离)
- 编辑距离(Minimum Edit Distance)
- LeetCode 72. Edit Distance(编辑距离)
- 线程控制(一)线程基础
- java-第5天
- 09全志r40m平台的SATA验证(分色排版)
- 《剑指offer》面试题17:打印从1到最大的n位数
- hadoop 更换数据目录
- hdu 1516 String Distance and Transform Process(编辑距离+记录路径)
- nginx做SSL并配合tomcat实现HTTPS访问
- 图像标注教程(使用LabelImg标注工具)
- asp.net数据库操作时出现错误 其他信息: 基础提供程序在 Open 上失败。
- Milking Cows 挤牛奶--贪心
- 【设计模式】(6)--最常用模式之观察者模式
- Photoshop中的一些简单操作及图片制作
- SSM POI---导出(含工具类)
- C#创建不规则窗体(窗口)的几种方式