C# 用c语言打印LevenshteInstance矩阵#
我已经在c#中实现了LevenshteIndication算法,如下所示。此代码运行良好。但出于调试目的,我想打印矩阵,但无法决定将print语句放在何处C# 用c语言打印LevenshteInstance矩阵#,c#,algorithm,C#,Algorithm,我已经在c#中实现了LevenshteIndication算法,如下所示。此代码运行良好。但出于调试目的,我想打印矩阵,但无法决定将print语句放在何处 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _Levenshtein_ { class Program { public static void Print(in
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _Levenshtein_
{
class Program
{
public static void Print(int[,] data)
{
for (int i = 0; i < data.GetUpperBound(0); i++)
{
for (int j = 0; j < data.GetUpperBound(1); j++)
{
Console.Write(data[i, j] + " ");
}
}
Console.WriteLine();
}
public static int LevenshteinDistance(string source, string target)
{
if (String.IsNullOrEmpty(source))
{
if (String.IsNullOrEmpty(target)) return 0;
{
return target.Length;
}
}
if (String.IsNullOrEmpty(target)) return source.Length;
if (source.Length > target.Length)
{
var temp = target;
target = source;
source = temp;
}
var m = target.Length;
var n = source.Length;
var distance = new int[2, m + 1];
// Initialize the distance 'matrix'
for (var j = 1; j <= m; j++) distance[0, j] = j;
Console.Write(target + " ");
var currentRow = 0;
for (var i = 1; i <= n; ++i)
{
currentRow = i & 1;
distance[currentRow, 0] = i;
var previousRow = currentRow ^ 1;
Console.WriteLine(source[i-1] + " " );
for (var j = 1; j <= m; j++)
{
var cost = (target[j - 1] == source[i - 1] ? 0 : 1);
distance[currentRow, j] = Math.Min(Math.Min(distance[previousRow, j] + 1,distance[currentRow, j - 1] + 1),distance[previousRow, j - 1] + cost);
Print(distance);
}
Console.WriteLine();
}
return distance[currentRow, m];
}
static void Main(string[] args)
{
LevenshteinDistance("Sunday", "Saturday");
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
名称空间_Levenshtein_
{
班级计划
{
公共静态无效打印(int[,]数据)
{
for(int i=0;itarget.Length)
{
var temp=目标;
目标=源;
源=温度;
}
var m=目标长度;
var n=源长度;
var距离=新整数[2,m+1];
//初始化距离“矩阵”
对于(var j=1;jI),我在打印距离矩阵中添加了注释
// `target` string in first ROW, each char in 4 width
// for (var j = 0; j <=target.Length; j++)
// Console.Write(target[j] + " ")
for (var i = 1; i <= n; ++i)
{
currentRow = i & 1;
distance[currentRow, 0] = i;
var previousRow = currentRow ^ 1;
// print: `source[i]` ith char only one Console.Write(source[i] + " ")
// Console.Write(source[i] + " ")
for (var j = 1; j <= m; j++)
{
var cost = (target[j - 1] == source[i - 1] ? 0 : 1);
distance[currentRow, j] = Math.M........);
// write distance in 3 width
//Console.Write(distance[currentRow, j] + " ")
}
// Console.Write("\n")
}
所以目标是“星期六”字符串。(水平字符串)
“星期天”是垂直的
您将得到如下图所示的输出:
下面是我的C代码,可以打印编辑距离矩阵,如:
S a t u r d a y
S 0 1 2 3 4 5 6 7
u 1 1 2 2 3 4 5 6
n 2 2 2 3 3 4 5 6
d 3 3 3 3 4 3 4 5
a 4 3 4 4 4 4 3 4
y 5 4 4 5 5 5 4 3
我在打印距离矩阵时添加了注释
// `target` string in first ROW, each char in 4 width
// for (var j = 0; j <=target.Length; j++)
// Console.Write(target[j] + " ")
for (var i = 1; i <= n; ++i)
{
currentRow = i & 1;
distance[currentRow, 0] = i;
var previousRow = currentRow ^ 1;
// print: `source[i]` ith char only one Console.Write(source[i] + " ")
// Console.Write(source[i] + " ")
for (var j = 1; j <= m; j++)
{
var cost = (target[j - 1] == source[i - 1] ? 0 : 1);
distance[currentRow, j] = Math.M........);
// write distance in 3 width
//Console.Write(distance[currentRow, j] + " ")
}
// Console.Write("\n")
}
所以目标是“星期六”字符串。(水平字符串)
“星期天”是垂直的
您将得到如下图所示的输出:
下面是我的C代码,可以打印编辑距离矩阵,如:
S a t u r d a y
S 0 1 2 3 4 5 6 7
u 1 1 2 2 3 4 5 6
n 2 2 2 3 3 4 5 6
d 3 3 3 3 4 3 4 5
a 4 3 4 4 4 4 3 4
y 5 4 4 5 5 5 4 3
你想在每个单元格打印带有分数的matix吗?@GrijeshChauhan:是的,你说得对。我没有C#背景,所以我只是显示写字符串的位置……等等。@GrijeshChauhan:没关系。你可以编辑我的问题。你想在每个单元格打印带有分数的matix吗?@GrijeshChauhan:是的,你说得对。我没有C#背景,所以我只是显示去哪里写字符串…等等。@GrijeshChauhan:没关系。你可以编辑我的问题打印的参数是什么method@geek打印可能不是方法我只是显示在哪里调用打印方法您有Console.write。@极客您想这样输出评论更新的答案吗?我按照您的说明更改了代码,但打印不正确o(非常感谢:)+1对于代码段,打印的参数是什么method@geek打印可能不是方法我只是告诉你在哪里调用打印方法你有控制台。write。@极客你想这样输出评论更新的答案吗?我按照你的说明更改了我的代码,但它没有打印正确的输出Hanks很多:)+1的代码片段