编程挑战--最小操作数(C#.Net版算法)
来源:互联网 发布:ipad能编辑淘宝店铺 编辑:程序博客网 时间:2024/06/07 16:17
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string a = "hit";
string temp = "cog";
string[] dic = { "hot", "dot", "dog", "lot", "log" };
if (a.Length != temp.Length)
{
Console.Write("没有匹配的转换");
Console.Read();
return;
}
if (a == temp)
{
Console.Write("两个字符串相等");
Console.Read();
return;
}
List<List<string>> lists = new List<List<string>>();
List<string> printList = new List<string>();
if (!Match(a, temp, dic, printList, lists))
{
Console.Write("没有匹配的转换");
}
foreach (List<string> list in lists)
{
int i = 1;
foreach (string s in list)
{
if (i == list.Count)
Console.Write(s);
else
{
Console.Write(s + ",");
}
i++;
}
Console.WriteLine();
}
Console.Read();
}
/// <summary>
/// 匹配引擎
/// </summary>
/// <param name="a"></param>
/// <param name="temp"></param>
/// <param name="dic"></param>
/// <param name="printList"></param>
/// <param name="lists"></param>
/// <returns></returns>
private static bool Match(string a, string temp, string[] dic, List<string> printList, List<List<string>> lists)
{
printList.Insert(0, temp);
bool isMatch = false;
int matchIndex = -1;
int aMatch = MatchingString(temp, a);
if (aMatch == 1)
{
printList.Insert(0, a);
if (lists.Count == 0) lists.Add(printList);
else if (printList.Count == lists[0].Count)
{
lists.Add(printList);
}
else if (printList.Count < lists[0].Count)
{
lists.Clear();
lists.Add(printList);
}
isMatch = true;
}
for (int i = 0; i < dic.Length; i++)
{
int match = MatchingString(temp, dic[i]);
if (match == 1)
{
matchIndex = i;
if (Match(a, dic[i], RemoveItem(i, dic), CopyString(printList), lists))
isMatch = true;
}
}
return isMatch;
}
/// <summary>
/// 复制一个List
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
private static List<string> CopyString(List<string> list)
{
List<string> newList = new List<string>();
foreach (string s in list)
{
newList.Add(s);
}
return newList;
}
/// <summary>
/// 匹配两个字符串,返回两个单词的差异数
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
private static int MatchingString(string a, string b)
{
int count = 0;
if (a.Length != b.Length) return 0;//字符串不相等,直接返回0
for (int i = 0; i < a.Length; i++)
{
if (a[i] != b[i])
{
count++;
}
}
return count;
}
/// <summary>
/// 从字符串中移除指定索引的元素,返回一个新字符串
/// </summary>
/// <param name="index"></param>
/// <param name="dic"></param>
/// <returns></returns>
private static string[] RemoveItem(int index, string[] dic)
{
string[] newArray = new string[dic.Length - 1];
int count = 0;
for (int i = 0; i < dic.Length; i++)
{
if (i == index) continue;
newArray[count] = dic[i];
count++;
}
return newArray;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*
题目详情
*/namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string a = "hit";
string temp = "cog";
string[] dic = { "hot", "dot", "dog", "lot", "log" };
if (a.Length != temp.Length)
{
Console.Write("没有匹配的转换");
Console.Read();
return;
}
if (a == temp)
{
Console.Write("两个字符串相等");
Console.Read();
return;
}
List<List<string>> lists = new List<List<string>>();
List<string> printList = new List<string>();
if (!Match(a, temp, dic, printList, lists))
{
Console.Write("没有匹配的转换");
}
foreach (List<string> list in lists)
{
int i = 1;
foreach (string s in list)
{
if (i == list.Count)
Console.Write(s);
else
{
Console.Write(s + ",");
}
i++;
}
Console.WriteLine();
}
Console.Read();
}
/// <summary>
/// 匹配引擎
/// </summary>
/// <param name="a"></param>
/// <param name="temp"></param>
/// <param name="dic"></param>
/// <param name="printList"></param>
/// <param name="lists"></param>
/// <returns></returns>
private static bool Match(string a, string temp, string[] dic, List<string> printList, List<List<string>> lists)
{
printList.Insert(0, temp);
bool isMatch = false;
int matchIndex = -1;
int aMatch = MatchingString(temp, a);
if (aMatch == 1)
{
printList.Insert(0, a);
if (lists.Count == 0) lists.Add(printList);
else if (printList.Count == lists[0].Count)
{
lists.Add(printList);
}
else if (printList.Count < lists[0].Count)
{
lists.Clear();
lists.Add(printList);
}
isMatch = true;
}
for (int i = 0; i < dic.Length; i++)
{
int match = MatchingString(temp, dic[i]);
if (match == 1)
{
matchIndex = i;
if (Match(a, dic[i], RemoveItem(i, dic), CopyString(printList), lists))
isMatch = true;
}
}
return isMatch;
}
/// <summary>
/// 复制一个List
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
private static List<string> CopyString(List<string> list)
{
List<string> newList = new List<string>();
foreach (string s in list)
{
newList.Add(s);
}
return newList;
}
/// <summary>
/// 匹配两个字符串,返回两个单词的差异数
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
private static int MatchingString(string a, string b)
{
int count = 0;
if (a.Length != b.Length) return 0;//字符串不相等,直接返回0
for (int i = 0; i < a.Length; i++)
{
if (a[i] != b[i])
{
count++;
}
}
return count;
}
/// <summary>
/// 从字符串中移除指定索引的元素,返回一个新字符串
/// </summary>
/// <param name="index"></param>
/// <param name="dic"></param>
/// <returns></returns>
private static string[] RemoveItem(int index, string[] dic)
{
string[] newArray = new string[dic.Length - 1];
int count = 0;
for (int i = 0; i < dic.Length; i++)
{
if (i == index) continue;
newArray[count] = dic[i];
count++;
}
return newArray;
}
}
}
- 编程挑战--最小操作数(C#.Net版算法)
- 挑战编程:最小操作数
- 编程算法 - 把数组排成最小的数 代码(C)
- Google编程题:最小操作数
- [PHP解算法题]最小操作数
- 编程算法 - 最小能被1至n整除的数 代码(C)
- 编程算法 - 最小能被1至n整除的数 代码(C)
- 最小操作数简易版
- 挑战庞果英雄会之最小操作数(失败求指教)
- C语言编程 用任何一种熟悉的方法描述求3个数中最小数的算法
- 最小操作数-c#求解-英雄会在线编程题目
- 庞果网的一个算法题目:最小操作数
- CSDN编程挑战:绝对值最小--Java实现
- CSDN挑战编程——《绝对值最小》
- CSDN挑战编程——《绝对值最小》
- 最小操作数
- 最小操作数
- 最小操作数
- android socket 聊天实现
- 从递归算法开始学习
- 如何将自己写的verilog模块封装成IP核
- C语言编程笔试题(一直更新)
- javax.xml.datatype.XMLGregorianCalendar
- 编程挑战--最小操作数(C#.Net版算法)
- Ubuntu 12.04 server 安装后可能需要更新 apt 源
- MUST Read
- ARM GCC 内嵌(inline)汇编手册
- 网站开发后台技术一
- 经典案例,二分查找的三种实现方式
- OSI七层模型基础知识及各层常见应用
- 黑马程序员_毕向东_JavaScript视频教程--学习笔记
- Hibernate事务与并发问题处理(乐观锁与悲观锁)