hdu1516(字符串距离+输出路径)
来源:互联网 发布:app交友软件排行 编辑:程序博客网 时间:2024/06/08 07:59
String Distance and Transform Process
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 447 Accepted Submission(s): 222
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
路径可以用字符串路径算法求出,字符串距离算法
代码:
#include <bits/stdc++.h>using namespace std;const int maxn=1000+5;char s[maxn],t[maxn];int dp[maxn][maxn];//表示a长度为maxn时装换到长度为maxn的b所需的最短操作int n,m;void input(){}void solve(){ n=strlen(s),m=strlen(t); for(int i=0;i<=n;i++) dp[i][0]=i; for(int i=0;i<=m;i++) dp[0][i]=i; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++){ dp[i][j]=min(dp[i-1][j],dp[i][j-1])+1; dp[i][j]=min(dp[i][j],dp[i-1][j-1]+(s[i-1]!=t[j-1])); }}void output(){ printf("%d\n",dp[n][m]); int i=strlen(s),j=strlen(t); int step=0; int temp; while(i>=1||j>=1){ if(s[i-1]!=t[j-1]) temp=1; else temp=0; if(dp[i][j]==(dp[i-1][j-1]+temp)&&i>=1&&j>=1){ if(temp) printf("%d Replace %d,%c\n",++step,i,t[j-1]); i--,j--; }else if(dp[i][j]==(dp[i][j-1]+1)&&j>=1){ printf("%d Insert %d %c\n",++step,i+1,t[j-1]); j--; }else if(dp[i][j]==(dp[i-1][j]+1)&&i>=1){ printf("%d Delete %d\n",++step,i); i--; } }}int main(){ while(~scanf("%s%s",s,t)){ solve(); output(); } return 0;}
阅读全文
0 0
- hdu1516(字符串距离+输出路径)
- hdu1516 字符串编辑距离dp
- 输出路径的最小编辑距离
- Levenshtein 距离(字符串距离)
- Palindromic Subsequence(最长回文字符串 输出路径)
- 计算字符串距离(编辑距离)
- 深入路径距离分析(一)
- 深入路径距离分析(二)
- 深入路径距离分析(三)
- (51Nod 1183 编辑距离)字符串编辑距离
- 【动态规划】字符串编辑距离(Levenshtein距离)算法
- 迷宫问题(输出路径)
- 路径输出问题-(DFS)
- 最短路(输出路径)
- LeetCode | Edit Distance(字符串编辑距离)
- Edit Distance 字符串距离(重重)
- sdutoj1225--编辑距离(dp:字符串转化)
- 华为OJ(计算字符串的距离)
- Spring整合Web项目原理
- 长沙理工大学第十二届ACM大赛 K.大家一起来数二叉树吧【Dp】
- 2.5 unity实战 飞机大战
- 1572:精挑细选
- CodeForces 110 A.Nearly Lucky Number(水~)
- hdu1516(字符串距离+输出路径)
- win10原版镜像和激活工具
- CSS — (盒子模型)
- mongoDB简介及安装
- 8.使用预定义标准BI Content抽取R3财务总账系统中的数据
- 2.6 Unity2D平台开发
- Vue.js 2.0 快速上手精华梳理
- 【九度OJ】1005:Graduate Admission
- 马云经典语录