计数变化数比较c#中的两个字符串

时间:2015-11-06 08:43:40

标签: c#

我想找出比较两个字符串的更改次数。 例如:

字符串1:

  

我希望从这个机会中做点好事。我想我的测试表格会   在ODI帮助我。无论如何,得分都在国际板球比赛中进行   格式,给予玩家信心。

字符串2:

  

"我希望这个机会做得很好。我想我的测试表格会   帮我ODI格式。得分在最新的国际板球比赛中进行,   无论格式如何,都能给予玩家信心。"

预期输出:5。(忽略空格和换行符)

变更是:

  1. from(从字符串2中删除),
  2. 测试(在字符串2中修改),
  3. 格式(字符串2中的额外添加),
  4. 最新(字符串2中的额外添加),
  5. 播放(在字符串2中修改)。
  6. 有计算更改次数的算法吗?

3 个答案:

答案 0 :(得分:1)

您的问题与比较两个文件非常相似。区别在于文件比较文件通过比较一行中的文本进行比较。在您的情况下,空格将是分隔符而不是换行符。

通常,这是通过找到最长公共子序列来完成的。这是一个相关文档,解释了它:https://nanohub.org/infrastructure/rappture/export/2719/trunk/gui/src/diff.pdf

寻找LCS:

 public static int GetLCS(string str1, string str2)
    {
        int[,] table;
        return GetLCSInternal(str1, str2, out table);
    }

    private static int GetLCSInternal(string str1, string str2, out int[,] matrix)
    {
        matrix = null;

        if (string.IsNullOrEmpty(str1) || string.IsNullOrEmpty(str2))
        {
            return 0;
        }

        int[,] table = new int[str1.Length + 1, str2.Length + 1];
        for (int i = 0; i < table.GetLength(0); i++)
        {
            table[i, 0] = 0;
        }
        for(int j= 0;j<table.GetLength(1); j++)
        {
            table[0,j] = 0;
        }

        for (int i = 1; i < table.GetLength(0); i++)
        {
            for (int j = 1; j < table.GetLength(1); j++)
            {
                if (str1[i-1] == str2[j-1])
                    table[i, j] = table[i - 1, j - 1] + 1;
                else
                {
                    if (table[i, j - 1] > table[i - 1, j])
                        table[i, j] = table[i, j - 1];
                    else
                        table[i, j] = table[i - 1, j];
                }
            }
        }

        matrix = table;
        return table[str1.Length, str2.Length];
    }

//读出按字典顺序排序的所有LCS

using System;
using System.Collections.Generic;
using System.Collections;
 using System.Linq;
using System.Text;

namespace LambdaPractice
{
class Program
{


    static int[,] c;

   static int max(int a, int b)
    {
        return (a > b) ? a : b;
    }

   static int LCS(string s1, string s2)
    {
        for (int i = 1; i <= s1.Length; i++)
            c[i,0] = 0;
        for (int i = 1; i <= s2.Length; i++)
            c[0, i] = 0;

        for (int i=1;i<=s1.Length;i++)
            for (int j = 1; j <= s2.Length; j++)
            {
                if (s1[i-1] == s2[j-1])
                    c[i, j] = c[i - 1, j - 1] + 1;
                else
                {
                    c[i, j] = max(c[i - 1, j], c[i, j - 1]);

                }

            }

        return c[s1.Length, s2.Length]; 

    }

/ *打印一个LCS        static string BackTrack(string s1,string s2,int i,int j)         {

        if (i == 0 || j == 0)
            return "";
        if (s1[i - 1] == s2[j - 1])
            return BackTrack(s1, s2, i - 1, j - 1) + s1[i - 1];
        else if (c[i - 1, j] > c[i, j - 1])
            return BackTrack(s1, s2, i - 1, j);
        else
            return BackTrack(s1, s2, i, j - 1);

    }*/

   static SortedSet<string> backtrack(string s1, string s2, int i, int j)
   {
       if (i == 0 || j == 0)
           return new SortedSet<string>(){""} ;
       else if (s1[i - 1] == s2[j - 1])
       {
           SortedSet<string> temp = new SortedSet<string>();
           SortedSet<string> holder = backtrack(s1, s2, i - 1, j - 1);
           if (holder.Count == 0)
           {
              temp.Add(s1[i - 1]);
           }
           foreach (string str in holder)

                temp.Add(str + s1[i - 1]);


           return temp;
       }
       else
       {
           SortedSet<string> Result = new SortedSet<string>() ;
           if (c[i - 1, j] >= c[i, j - 1])
           {
               SortedSet<string> holder = backtrack(s1, s2, i - 1, j);

               foreach (string s in holder)
                   Result.Add(s);
           }

           if (c[i, j - 1] >= c[i - 1, j])
           {
               SortedSet<string> holder = backtrack(s1, s2, i, j - 1);

               foreach (string s in holder)
                   Result.Add(s);
           }


           return Result;
       }

   }

    static void Main(string[] args)
    {
            string s1, s2;
            s1 = Console.ReadLine();
            s2 = Console.ReadLine();
            c = new int[s1.Length+1, s2.Length+1];
            LCS(s1, s2);
            // Console.WriteLine(BackTrack(s1, s2, s1.Length, s2.Length));
            //  Console.WriteLine(s1.Length);
            SortedSet<string> st = backtrack(s1, s2, s1.Length, s2.Length);

            foreach (string str in st)
                Console.WriteLine(str);
            GC.Collect();

       Console.ReadLine();
    }
}
}

答案 1 :(得分:0)

你可以检查这个might be useful somehow,我会说你会使用很多控制语句(if / else或switch)来结果集。

答案 2 :(得分:0)

如果您将字符串视为modified,如果它以另一个字符串开头,那么您可以相对容易地做到这一点:

void Main()
{
    var a = "I hope to do something good from this chance.I think my Test form will help me in ODI.Scoring runs in international cricket, regardless of the format, gives a player confidence.";

    var b = "I hope to do something good this chance. I think my Testing form will help me in ODI Format. Scoring runs in latest international cricket, regardless of the format, gives a playing confidence.";

    var d = new Difference(a,b);

    Console.WriteLine("Number of differences: {0}", d.Count);

    foreach (var diff in d.Differences)
    {
        Console.WriteLine("Different: {0}", diff);
    }
}

class Difference
{
    string a;
    string b;

    List<string> notInA;
    List<string> notInB;

    public int Count
    {
        get { return notInA.Count + notInB.Count; }
    }

    public IEnumerable<string> Differences
    {
        get { return notInA.Concat(notInB); }
    }

    public Difference(string a, string b)
    {
        this.a = a;
        this.b = b;

        var itemsA = Split(a);
        var itemsB = Split(b);

        var changedPairs =
            from x in itemsA
            from y in itemsB
            where (x.StartsWith(y) || y.StartsWith(x)) && y != x
            select new { x, y };

        var softChanged = changedPairs.SelectMany(p => new[] {p.x, p.y}).Distinct().ToList();

        notInA = itemsA.Except(itemsB).Except(softChanged).ToList();
        notInB = itemsB.Except(itemsA).Except(softChanged).ToList();
    }

    IEnumerable<string> Split(string x)
    {
        return x.Split(new[] { " ", ".", ","}, StringSplitOptions.RemoveEmptyEntries);
    }
}

输出:

Number of differences: 5
Different: from
Different: player
Different: Format
Different: latest
Different: playing