HDU 1516 String Distance and Transform Process
http://blog.csdn.net/u013081425/article/details/20912633
还是编辑距离,这里要同时输出编辑过程。开始我以为只能正着输出编辑过程,dfs了很久,其实分别对两个字符串根据dp[i][j]的变化逆推回去就可以A。
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
abcac bcd aaa aabaaaa
Sample Output
3 1 Delete 1 2 Replace 3,d 3 Delete 4 4 1 Insert 1,a 2 Insert 2,a 3 Insert 3,b 4 Insert 7,a
还是编辑距离,这里要同时输出编辑过程。开始我以为只能正着输出编辑过程,dfs了很久,其实分别对两个字符串根据dp[i][j]的变化逆推回去就可以A。
- char s1[100],s2[100];
- int dp[100][100];
- int step;
- int len1,len2;
- void solve()
- {
- 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++)
- {
- if(s1[i-1] == s2[j-1])
- dp[i][j] = dp[i-1][j-1];
- else
- dp[i][j] = min(min(dp[i][j-1]+1, dp[i-1][j]+1),dp[i-1][j-1]+1);
- }
- }
- }
- void path()
- {
- int tmp;
- int i,j;
- for(i = len1,j = len2;;)
- {
- if(i < 1 && j < 1)
- break;
- if(s1[i-1] == s2[j-1])
- tmp = 0;
- else tmp = 1;
- if(dp[i][j] == dp[i-1][j-1]+tmp && i >= 1 && j >= 1)
- {
- if(tmp) //替换,i--,j--;
- printf("%d Replace %d,%c\n",++step,i,s2[j-1]);
- i--;
- j--;
- }
- else if(dp[i][j] == dp[i-1][j]+1 && i >= 1)//删除i,i--
- {
- printf("%d Delete %d\n",++step,i);
- i--;
- }
- else if(dp[i][j] == dp[i][j-1]+1 && j >= 1)//增加i+1处,j--
- {
- printf("%d Insert %d,%c\n",++step,i+1,s2[j-1]);
- j--;
- }
- }
- }
- int main()
- {
- while(~scanf("%s %s",s1,s2))
- {
- len1 = strlen(s1);
- len2 = strlen(s2);
- solve();
- printf("%d\n",dp[len1][len2]);
- step = 0;
- path();
- }
- return 0;
- }