• 大小: 22KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-29
  • 语言: C#
  • 标签: C#  LCS  算法  

资源简介

最长公共子序列,即LCS算法,用C#写的LCS算法实现过程

资源截图

代码片段和文件信息

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

namespace ConsoleApplication1
{
    class Program
    {
        private static int count = 0;
        public static void Main(string[] args)
        {
            String s1 = “abcdefg“;
            String s2 = “bdccae“;
            getLCSLength(s1 s2);
            
            Console.WriteLine(count);
            Console.ReadKey();
        }

        public static void getLCSLength(String str1 String str2)
    {
    char[] x = str1.ToCharArray();
    char[] y = str2.ToCharArray();

    int[] c = new int[x.Length+1y.Length+1];

    for(int i=1; i     for(int j=1; j     {
    if(x[i-1] == y[j-1])
    c[ij] = c[i-1j-1]+1;
    else if(c[i-1j]>=c[ij-1])
    c[ij] = c[i-1j];
    else
    c[ij] = c[ij-1];
    }
    printLCS(c x y  x.Length y.Length);
    }

        public static void printLCS(int[] c char[] x char[] y int i int j)
        {
            //int count = 0;
            if (i == 0 || j == 0)
                return;
            if (x[i - 1] == y[j - 1])
            {
                printLCS(c x y i - 1 j - 1);
                Console.WriteLine(x[i - 1]);
                count++;
            }
            else if (c[i - 1j] >= c[ij - 1])
                printLCS(c x y i - 1 j);
            else
                printLCS(c x y i j - 1);
        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       5632  2013-05-14 10:46  LCS\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe

     文件      13824  2013-05-14 10:46  LCS\ConsoleApplication1\bin\Debug\ConsoleApplication1.pdb

     文件      11600  2013-05-14 13:18  LCS\ConsoleApplication1\bin\Debug\ConsoleApplication1.vshost.exe

     文件        490  2009-08-31 00:40  LCS\ConsoleApplication1\bin\Debug\ConsoleApplication1.vshost.exe.manifest

     文件       2459  2013-05-08 09:06  LCS\ConsoleApplication1\ConsoleApplication1.csproj

     文件        928  2013-05-14 13:18  LCS\ConsoleApplication1\obj\x86\Debug\ConsoleApplication1.csproj.FileListAbsolute.txt

     文件       5632  2013-05-14 10:46  LCS\ConsoleApplication1\obj\x86\Debug\ConsoleApplication1.exe

     文件      13824  2013-05-14 10:46  LCS\ConsoleApplication1\obj\x86\Debug\ConsoleApplication1.pdb

     文件       5798  2013-05-14 10:46  LCS\ConsoleApplication1\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件       1553  2013-05-14 10:46  LCS\ConsoleApplication1\Program.cs

     文件       1370  2013-05-08 09:06  LCS\ConsoleApplication1\Properties\AssemblyInfo.cs

     文件        899  2013-05-08 09:06  LCS\ConsoleApplication1.sln

    ..A..H.     16384  2013-05-14 13:53  LCS\ConsoleApplication1.suo

     目录          0  2013-05-08 09:06  LCS\ConsoleApplication1\obj\x86\Debug\TempPE

     目录          0  2013-05-14 10:46  LCS\ConsoleApplication1\obj\x86\Debug

     目录          0  2013-05-08 09:12  LCS\ConsoleApplication1\bin\Debug

     目录          0  2013-05-08 09:06  LCS\ConsoleApplication1\obj\x86

     目录          0  2013-05-08 09:06  LCS\ConsoleApplication1\bin

     目录          0  2013-05-08 09:06  LCS\ConsoleApplication1\obj

     目录          0  2013-05-08 09:06  LCS\ConsoleApplication1\Properties

     目录          0  2013-05-08 09:06  LCS\ConsoleApplication1

     目录          0  2013-05-08 09:06  LCS

----------- ---------  ---------- -----  ----

                80393                    22


评论

共有 条评论