111,092
社区成员




using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static List<string> Dict;
static void Main(string[] args)
{
StreamReader objReader = new StreamReader("c:\\source.txt");
Dict = DictGen.ParseString(objReader).ToList();
string s1 = "";
while (s1 == "")
{
Console.WriteLine("Please input a word:");
s1 = Console.ReadLine();
}
CompareString cs = new CompareString();
var match = Dict.AsParallel().Select(x => new { key = cs.Compare(x, s1), value = x })
.OrderBy(x => x.key)
.ThenBy(x => x.value)
.Where(x => x.key <= (s1.Length / 2 == 0 ? 1 : s1.Length / 2));
match.ToList().ForEach(x => Console.WriteLine(x.value + "\t\t" + x.key.ToString()));
}
}
class CompareString
{
public int Compare(string S, string D)
{
if (string.IsNullOrEmpty(S) || string.IsNullOrEmpty(D))
throw new Exception("String should not be empty.");
int[,] array = new int[S.Length + 1, D.Length + 1];
for (int i = 0; i <= S.Length; i++)
array[i, 0] = i;
for (int i = 1; i <= D.Length; i++)
array[0, i] = i;
for (int i = 1; i <= S.Length; i++)
{
for (int j = 1; j <= D.Length; j++)
{
int c = S[i - 1] == D[j - 1] ? 0 : 1;
int n1 = array[i - 1, j] + 1;
int n2 = array[i - 1, j - 1] + c;
int n3 = array[i, j - 1] + 1;
int v = n1 < n2 ? n1 : n2;
array[i, j] = v < n3 ? v : n3;
}
}
return array[S.Length, D.Length];
}
}
class DictGen
{
class Node
{
public Dictionary<char, Node> Dict { get; set; }
public Node() { Dict = new Dictionary<char, Node>(); }
public IEnumerable<string> Get()
{
foreach (var item in Dict.OrderBy(x => x.Key))
{
if (item.Key == '\0') yield return "";
foreach (var item1 in item.Value.Get())
{
yield return item.Key.ToString() + item1;
}
}
}
}
public static IEnumerable<string> ParseString(TextReader tr)
{
Node rootNode = new Node();
Node currNode = rootNode;
int state = 0;
char[] arr;
try
{
while (true)
{
arr = tr.ReadLine().ToLower().ToCharArray();
for (int i = 0; i < arr.GetLength(0); i++)
{
if (arr[i] >= 'a' && arr[i] <= 'z')
{
state = 1;
if (!currNode.Dict.ContainsKey(arr[i]))
currNode.Dict.Add(arr[i], new Node());
currNode = currNode.Dict[arr[i]];
}
else
{
if (state == 1)
{
if (!currNode.Dict.ContainsKey('\0'))
currNode.Dict.Add('\0', new Node());
state = 0;
}
currNode = rootNode;
}
}
}
}
catch
{ }
return rootNode.Get();
}
}
}
string s = "zs151as12a51s12";
for (int i = (int)'a'; i <= (int)'z'; i++)
s = s.Replace((char)i, ',');
int sum = 0;
foreach (var item in s.Split(','))
if (item != "")
sum += int.Parse(item);
Console.WriteLine(sum);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
String str = "a444a44asd343asd41a";
int num = 0;
for (int i = 0; i < str.Length; i++)
{
if (char.IsNumber(str[i]))
{
int j = 0;
String temp = null;
for (j = i; j < str.Length; j++)
{
if (char.IsNumber(str[j]) == false)
break;
temp += str[j].ToString();
}
i = j;
num += Int32.Parse(temp);
Console.Write("提取出来的数字为:");
Console.WriteLine(temp);
}
}
Console.WriteLine("和为:"+num);
Console.Read();
}
}
}