用三叉树ac了一个字典树,空间的确有所下降

hdu的1251
上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import java.util.*;
/**
 *
 * @author yanglingfeng
 */
 class TernaryTree {
 
    private class Node {
 
        char m_char;
        Node left, right, middle;
        boolean end;
        int co;
 
        public Node(char ch, boolean e) {
            m_char = ch;
            end = e;
            co=0;
        }
 
        public Node(char ch, boolean e, Node leftc, Node rightc, Node middlec) {
            m_char = ch;
            end = e;
            left = leftc;
            right = rightc;
            middle = middlec;
        }
    }
    private Node root;
    private int count;
 
    /**
     * 初始化三叉树
     */
    public TernaryTree() {
        clear();
    }
 
    /**
     * 清除三叉树
     */
    public final void clear() {
        root = null;
        count = 0;
    }
 
    /**
     * 插入一个字符串
     * @param s
     */
    public void insert(String s) {
        root = insert(root, s, 0);
    }
 
    /**
     * 返回三叉树大小
     * @return
     */
    public int size() {
        return count;
    }
 
    /**
     * 返回是否能够找到字符串s
     * @param s
     * @return boolean
     */
    public boolean contains(String s) {
        return search(root, s, 0) != null;
    }
    public int count(String s) {
        Node a= search(root, s, 0);
        if(a!=null){
            return a.co;
        }else
            return 0;
    }
 
    private Node search(Node ref, String s, int pos) {
        if (ref == null) {
            return null;
        } else {
            int cmp = s.charAt(pos) - ref.m_char;
            if (cmp < 0) {
                return search(ref.left, s, pos);
            } else if (cmp > 0) {
                return search(ref.right, s, pos);
            } else {
                if (1 + pos == s.length()) {
                    return ref;
                }
                return search(ref.middle, s, pos + 1);
            }
        }
    }
 
    private Node insert(Node ref, String s, int i) {
        if (ref == null) {
            count++;
            ref = new Node(s.charAt(i), false);
        }
        int cmp = s.charAt(i) - ref.m_char;
        if (cmp < 0) {
            ref.left = insert(ref.left, s, i);
        } else if (cmp > 0) {
            ref.right = insert(ref.right, s, i);
        } else {
            if (i + 1 == s.length()) {
                ref.end = true;
            } else {
                ref.middle = insert(ref.middle, s, i + 1);
            }
            ref.co++;
        }
        return ref;
 
    }
}
public class Main {
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        TernaryTree dic=new TernaryTree();
        Scanner inp=new Scanner(System.in);
        String a=new String();
        int mo=0;
        while(inp.hasNextLine()){
            a=inp.nextLine();
            if(a.equals("")){
                mo=1;
                continue;
            }
            if(mo==0)
            {
                dic.insert(a);
            }else
            {
                System.out.println(dic.count(a));
            }
        }
    }
}

No Comments yet »

这篇文章上的评论 RSS feed TrackBack URI

留下评论

abuseangeraskavariceblacknessbyecannotcarnalitycoldcollapsecrazycriescruelCryingcussdizzygivegoodidleinnocentinvalidKaddishlikabilitylovenauseanosepossprositpudencyredcardrescuesighsleepsmartsmokesnubstrivesweatwelfareyellowcardyock