Germey
3/22/2019 - 2:23 AM

lcs

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

namespace LCS
{
    public class Element
    {
        public string Text { get; set; }

        public int Aindex { get; set; }

        public int Bindex { get; set; }
    }

    public class Program
    {

        static List<Element> LCS(string s1, string s2)
        {
            List<Element> list = new List<Element>();

            int[,] map = new int[s1.Length + 1, s2.Length + 1];

            int i, j;

            for (i = 1; i <= s1.Length; i++)
            {
                for (j = 1; j <= s2.Length; j++)
                {
                    if (s1[i - 1].Equals(s2[j - 1]))
                    {
                        map[i, j] = map[i - 1, j - 1] + 1;
                    }
                    else
                    {
                        map[i, j] = Math.Max(map[i - 1, j], map[i, j - 1]);
                    }
                }
            }

            i = s1.Length; j = s2.Length;

            List<Element> elements = new List<Element>();
            while (i > 0 && j > 0)
            {
                if (s1[i - 1].Equals(s2[j - 1]))
                {
                    elements.Add(new Element()
                    {
                        Text = s1[i - 1].ToString(),
                        Aindex = i - 1,
                        Bindex = j - 1
                    });
                    i -= 1;
                    j -= 1;
                }
                else if (map[i - 1, j] > map[i, j - 1])
                {
                    i -= 1;
                }
                else
                {
                    j -= 1;
                }

            }

            elements.Reverse();

            Console.WriteLine(elements[0].Text);

            return list;
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");


            string s1 = "你好啊卧室谁";
            string s2 = "好啊谁在";

            var result = LCS(s1, s2);

            Console.ReadKey();

        }

    }
}