static void Main(string[] args) //程序从main函数开始运行 { int[] L1 = { 34, 72, 13, 44, 25, 30, 10 }; //自定义的数组 int[] L2 = { 34, 13, 44, 7, 25 }; string[] P = { "我", "的", "名字", "是", "刘", "一", "达" }; //自定义的对象 string[] Q = { "刘", "一达", "是", "个", "好", "名字" }; LCS lcs = new LCS(); //实例化一个lcs对象 lcs.LCS_LENGTH(L1, L2); //整形情况 for (int i = 0; i < L1.Length; i++) //遍历这个矩阵依次打印回溯左上角的元素 { for (int j = 0; j < L2.Length; j++) { if (lcs.b[i, j] == "left_up") { Console.WriteLine("{0}", L1[i]); } } } lcs.LCS_LENGTH1(P, Q); //字符串情况 for (int i = 0; i < P.Length; i++) { for (int j = 0; j < Q.Length; j++) { if (lcs.b[i, j] == "left_up") { Console.WriteLine("{0}", P[i]); } } } Console.ReadLine(); }
/// <summary> /// 从index开始在strLCS中往后搜索替换字符串 /// </summary> /// <param name="index">开始搜索的位置</param> /// <param name="strLCS">LCS串</param> /// <param name="strBefore">返回:原字符串,如果未找到替换,strBefore为空</param> /// <param name="strAfter">返回:替换目标字符串</param> /// <returns>返回最后的Item的下一个Item的位置</returns> static int GetNextReplace(int index, LCS <char> strLCS, ref string strBefore, ref string strAfter) { //ITEM_MODE.X表示源文件中的原始文字,这里是原题,ITEM_MODE.Y表示目标文件中的替换后的新文字,这里是答案。 //本实验只有两种情况 //情况1,全文替换文字:先出现ITEM_MODE.Y后出现ITEM_MODE.X //情况2,全文删除文字:直接出现ITEM_MODE.X //出现ITEM_MODE.Y后没有出现ITEM_MODE.X表示增加文字,不是本实验研究范围。 strBefore = null; strAfter = null; int i; for (i = index; i < strLCS.Items.Length; i++) { Item <char> item = strLCS.Items[i]; if (item.Mode == ITEM_MODE.Y) { //Console.WriteLine(item); //测试用 //如果遇到下一组替换,本次替换结束 if (strBefore != null) { break; } strAfter += item.Value; } else if (item.Mode == ITEM_MODE.X) { //Console.WriteLine(item);//测试用 strBefore += item.Value; } else { if (strBefore != null) { break; } else if (strAfter != null)//如果只是增加,不认为是替换,继续往后找 { strAfter = null; } } } return(i); }
static void Main(string[] args) { int[] intList1 = { 34, 72, 13, 44, 25, 30, 10 }; int[] intList2 = { 34, 13, 44, 7, 25 }; string[] strList1 = { "abc", "def", "gh", "zwd" }; string[] strList2 = { "abc", "2", "def", "gh", "zwd" }; Rectangle[] recList1 = { new Rectangle(1.4, 2, UnitValues.m), new Rectangle(1.4, 2, UnitValues.m), new Rectangle(1, 2, UnitValues.m), new Rectangle(1.4, 2, UnitValues.m) }; Rectangle[] recList2 = { new Rectangle(1.4, 2, UnitValues.m), new Rectangle(140, 200, UnitValues.cm), new Rectangle(1.4, 2, UnitValues.m), new Rectangle(1.4, 2, UnitValues.m) }; Person[] personList1 = { new Person("zhang", 31), new Person("wang", 12) }; Person[] personList2 = { new Person("wang", 12) }; LCS <int> intLCS = new LCS <int>(intList1, intList2); LCS <string> strLCS = new LCS <string>(strList1, strList2); LCS <Rectangle> recLCS = new LCS <Rectangle>(recList1, recList2); LCS <Person> personLCS = new LCS <Person>(personList1, personList2); intLCS.Demo(); strLCS.Demo(); recLCS.Demo(); personLCS.Demo(); Console.ReadLine(); }
// public void symbol(char a) // { // LCS.LCS_LENGTH(X,Y); // } static void Main(string[] args) //程序从main函数开始运行 { int[] L1 = { 14, 56, 13, 44, 25, 30, 10, 7 }; //自定义的数组 int[] L2 = { 14, 13, 44, 7, 25 }; string[] M = { "我", "不是", "名", "一个", "侦", "探", "柯", "南" }; //自定义的对象 string[] N = { "名", "侦", "探", "柯", "南", "小胖" }; LCS lcs = new LCS(); //实例化一个lcs对象 lcs.LCS_LENGTH(L1, L2); //运用lcs类中的方法 for (int i = 0; i < L1.Length; i++) //遍历这个矩阵依次打印回溯左上角的元素 { for (int j = 0; j < L2.Length; j++) { // lcs.LCSW(lcs.a,lcs.b, L1, i, j); // Console.WriteLine("{0}", L1[i]); if (lcs.b[i, j] == "left_up") { Console.WriteLine("{0}", L1[i]); } // else if (lcs.b[i, j] == "up") // Console.WriteLine("{0}", L1[i]); // else // Console.WriteLine("{0}", L1[i]); } } lcs.LCS_LENGTH1(M, N); //同理 for (int i = 0; i < M.Length; i++) { for (int j = 0; j < N.Length; j++) { //lcs.LCSP(lcs.b, list1, i, j); if (lcs.b[i, j] == "left_up") { Console.WriteLine("{0}", M[i]); } } } Console.ReadLine(); }
static void Main(string[] args) { //替换前的字符串 string strBefore = null; //替换后的字符串 string strAfter = null; //替换出现的次数 int Count = 0; EWordDocument eWordDocument原题 = new EWordDocument(); eWordDocument原题.Open(@"D:\教学\2018-2019\农业研究生C#\CSharp\test3\国考_原题.docx"); EWordDocument eWordDocument答案 = new EWordDocument(); eWordDocument答案.Open(@"D:\教学\2018-2019\农业研究生C#\CSharp\test3\国考_标准答案1.docx"); int idx; for (int i = 0; i < eWordDocument原题.LText.Count(); i++) { char[] arrayX = eWordDocument原题.LText[i].ToArray(); char[] arrayY = eWordDocument答案.LText[i].ToArray(); LCS <char> strLCS = new LCS <char>(arrayX, arrayY); idx = 0; string strBefore_ = null; string strAfter_ = null; while (idx < strLCS.Items.Length) { //如果未找到替换,strBefore为空 if (strBefore == null) { idx = GetNextReplace(idx, strLCS, ref strBefore, ref strAfter); if (strBefore != null) { Count++; } } else { idx = GetNextReplace(idx, strLCS, ref strBefore_, ref strAfter_); if (strBefore == strBefore_ && strAfter == strAfter_) { Count++; } } } } if (strBefore != null) { if (strAfter != null) { Console.WriteLine("替换题:请将文中所有的文字“{0}”替换为“{1}”。总分:{2}分", strBefore, strAfter, Count); } else { Console.WriteLine("替换题:请删除文中所有的文字“{0}”。总分:{1}分", strBefore, Count); } } else { Console.WriteLine("没有替换题!"); } Console.ReadLine(); }