110,545
社区成员
发帖
与我相关
我的任务
分享
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().ToLower() + 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().ToUpper().ToCharArray();
for (int i = 0; i < arr.GetLength(0); i++)
{
if (arr[i] >= 65 && arr[i] <= 90)
{
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();
}
}
//重新放在ArrayList里了
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace ConsoleApplication1
{
class Program
{
public class Node
{
public Node[] cld;
public bool have;
public Node()
{
cld = new Node[26];
for (int i = 0; i < 26; ++i)
cld[i] = null;
have = false;
}
public Node forward(int x)
{
if (cld[x] != null) return cld[x];
return cld[x] = new Node();
}
}
public void dfs(Node now, string build, ArrayList ret)
{
if (now.have == true) ret.Add(build);
for (int i = 0; i < 26; ++i) if (now.cld[i] != null)
{
dfs(now.cld[i], string.Copy(build) + (char)(i+'a'), ret);
}
}
public void parse()
{
StreamReader objReader = new StreamReader("D:\\source.txt");
string s = objReader.ReadToEnd();
objReader.Close();
Node root = new Node();
Node curr = root;
for (int i = 0; i < s.Length; ++i)
{
int who = s[i];
if (who >= 'A' && who <= 'Z') who |= 32;
if (who < 'a' || who > 'z')
{
if (curr != root)
{
curr.have = true;
curr = root;
}
}
else
{
curr = curr.forward(who-'a');
}
}
ArrayList ans = new ArrayList();
dfs(root, "", ans);
System.Console.Write("{0} elements have been found!\n", ans.Count);
//foreach(var x in ans) System.Console.WriteLine(x);
}
static void Main(string[] args)
{
long l = DateTime.Now.Ticks;
(new Program()).parse();
TimeSpan ts = new TimeSpan(DateTime.Now.Ticks - l);
MessageBox.Show(ts.ToString());
}
}
}
//不大熟悉C#参考了一些楼上的代码,好不容易拼出来的程序。
//计时不包括控制台的输出,否则把注释掉的打开就行
//to 11楼我的结果和lcl_data的结果,在你发出来的部分是一样的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace ConsoleApplication1
{
class Program
{
public class Node
{
public Node[] cld;
public bool have;
public Node()
{
cld = new Node[26];
for (int i = 0; i < 26; ++i)
cld[i] = null;
have = false;
}
public Node forward(int x)
{
if (cld[x] != null) return cld[x];
return cld[x] = new Node();
}
}
public void dfs(Node now, string build)
{
if (now.have == true) System.Console.WriteLine(build);
for (int i = 0; i < 26; ++i) if (now.cld[i] != null)
{
dfs(now.cld[i], string.Copy(build) + (char)(i+'a'));
}
}
public void parse()
{
StreamReader objReader = new StreamReader("D:\\source.txt");
string s = objReader.ReadToEnd();
objReader.Close();
Node root = new Node();
Node curr = root;
for (int i = 0; i < s.Length; ++i)
{
int who = s[i];
if (who >= 'A' && who <= 'Z') who |= 32;
if (who < 'a' || who > 'z')
{
if (curr != root)
{
curr.have = true;
curr = root;
}
}
else
{
curr = curr.forward(who-'a');
}
}
//dfs(root, "");
}
static void Main(string[] args)
{
long l = DateTime.Now.Ticks;
(new Program()).parse();
TimeSpan ts = new TimeSpan(DateTime.Now.Ticks - l);
MessageBox.Show(ts.ToString());
}
}
}
public static IEnumerable<string> ParseString1(TextReader tr)
{
return ParseString2(tr).Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Distinct().OrderBy(i => i);
}
public static string ParseString2(TextReader tr)
{
var s = tr.ReadToEnd().ToCharArray();
for (var x = 0; x < s.Length; x++)
if (s[x] >= 65 && s[x] <= 90)
s[x] += (char)32;
else if (s[x] < 97 || s[x] > 122)
s[x] = ' ';
return new string(s);
}