最近你画我猜挺火的,于是就写了个辅助工具。
输入候选字符,以及单词长度,就可以匹配出所有符合的单词
使用Trie树改造了下,6W单词,虽然还有一个剪枝没有加,但是还是秒出,速度挺快的。
Trie树:
package org.huohua.drawsomething;
import java.util.*;
class TrieNode
{
TrieNode []m_child;
boolean m_end;
int m_maxdeep;
static Set<String> s_Result = new HashSet<String>();
TrieNode()
{
m_child = new TrieNode[26];
m_end = false;
m_maxdeep = 0;
}
int Insert(String word,int index)
{
if(index == word.length())
{
m_end =true;
return 0;
}
int sub = word.charAt(index)-'a';
if(m_child[sub] ==null)
{
m_child[sub] = new TrieNode();
}
int deep = m_child[sub].Insert(word, index+1);
if (deep+1 > m_maxdeep) m_maxdeep = deep+1;
return deep+1;
}
boolean Find(String word,int index)
{
if(index == word.length())
{
return m_end;
}
int sub = word.charAt(index)-'a';
if (sub>25 || sub<0)
return false;
if (m_child[sub] ==null)
return false;
return m_child[sub].Find(word, index+1);
}
boolean FindDrawSomething(String chars,int num,String cur)
{
if(m_maxdeep < num)
return false;
if (num==0)
{
if(m_end)
{
s_Result.add(cur);
return true;
}
return false;
}
boolean f = false;
for(int i=0; i<chars.length(); ++i)
{
int sub = chars.charAt(i)-'a';
if (sub>25 || sub<0)
continue;
if (m_child[sub] ==null)
continue;
String nextchars = chars.substring(0,i)+chars.substring(i+1);
boolean ret = m_child[sub].FindDrawSomething(nextchars,num-1,cur+chars.charAt(i));
if(ret)
f = true;
}
return f;
}
}
public class TrieTree
{
TrieNode m_root;
TrieTree()
{
m_root = new TrieNode();
}
void Insert(String word)
{
m_root.Insert(word,0);
}
void Build(ArrayList<String> words)
{
m_root = new TrieNode();
for(String word :words)
{
Insert(word.toLowerCase());
}
}
boolean Find(String word)
{
return m_root.Find(word.toLowerCase(), 0);
}
boolean FindDrawSomething(String chars,int num)
{
if(chars.length() < num)
return false;
TrieNode.s_Result.clear();
return m_root.FindDrawSomething(chars.toLowerCase(), num,"");
}
}
Test
有个单词字典,是从别的软件下面找的,有6W单词
package org.huohua.drawsomething;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("E:\\HuohuaWorkspace\\java\\servlettest\\src\\org\\huohua\\drawsomething\\words3.dic"));
String word =null;
ArrayList<String> words = new ArrayList<String>();
while((word = br.readLine())!=null)
{
words.add(word);
}
System.out.println(words.size());
TrieTree tree = new TrieTree();
tree.Build(words);
System.out.println(tree.m_root.m_maxdeep);
Scanner sc =new Scanner(System.in);
while(sc.hasNext())
{
String s = sc.next();
int num = sc.nextInt();
System.out.println(tree.FindDrawSomething(s,num));
for(Iterator<String> it = TrieNode.s_Result.iterator(); it.hasNext(); )
{
System.out.println(it.next());
}
}
}
}
然后还写成了serverlet服务了
package org.huohua.drawsomething;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.huohua.drawsomething.*;;
public class Main extends HttpServlet {
TrieTree tree = new TrieTree();
public void init(ServletConfig config)
{
try {
BufferedReader br = new BufferedReader(new FileReader("E:\\HuohuaWorkspace\\java\\servlettest\\src\\org\\huohua\\drawsomething\\words3.dic"));
String word =null;
ArrayList<String> words = new ArrayList<String>();
while((word = br.readLine())!=null)
{
words.add(word);
}
tree.Build(words);
super.init(config);
} catch (Exception e) {
e.printStackTrace();
}
}
private void ShowFirst(HttpServletRequest request, HttpServletResponse response) throws IOException
{
PrintWriter out=response.getWriter();
out.println("<html><head><title>DrawSomethingHelper</title></head><body><form action=\"\"><input name=\"chars\"></input><input name=\"num\"></input><input type=\"submit\"></input></form></body></html>");
out.flush();
}
private void ShowQuery(HttpServletRequest request, HttpServletResponse response) throws IOException
{
Map<String,String[]> mapParams = request.getParameterMap();
PrintWriter out=response.getWriter();
String strChars = mapParams.get("chars")[0];
String strNum = mapParams.get("num")[0];
out.println("<html><head><title>DrawSomethingHelper</title></head><body><form action=\"\">CharSet<input name=\"chars\" value=\""+strChars+"\">Length</input><input name=\"num\" value=\""+strNum+"\"></input><input type=\"submit\" ></input></form></body></html>");
tree.FindDrawSomething(strChars,Integer.parseInt(strNum));
for(Iterator<String> it = TrieNode.s_Result.iterator(); it.hasNext(); )
{
out.println(it.next());
}
out.flush();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
Map<String,String[]> mapParams = request.getParameterMap();
if (mapParams.containsKey("chars") && mapParams.containsKey("num"))
ShowQuery(request,response);
else
ShowFirst(request,response);
}
}