ACM HDOJ 1251 (统计难题 )
来源:互联网 发布:dbc数据库详解 编辑:程序博客网 时间:2024/05/17 08:13
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1251
程序一 字典树
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner scn = new Scanner(System.in);Trie trie = new Trie();while (scn.hasNext()) {String word = scn.nextLine();if (0 == word.length()) {break;}trie.insert(word);}while (scn.hasNext()) {String word = scn.nextLine();System.out.println(trie.search(word));}scn.close();}}class Trie {private Node root;public Trie() {root = new Node(new Node[26], 0);}public void insert(String word) {Node current = root;for (int i = 0; i < word.length(); ++i) {if (null != current.getChildrenItem(word.charAt(i) - 'a')) {current = current.getChildrenItem(word.charAt(i) - 'a');current.setCount(current.getCount() + 1);} else {Node newNode = new Node(new Node[26], 1);current.setChildrenItem(word.charAt(i) - 'a', newNode);current = newNode;}}}public int search(String word) {Node current = root;for (int i = 0; i < word.length(); ++i) {if (null != current.getChildrenItem(word.charAt(i) - 'a')) {current = current.getChildrenItem(word.charAt(i) - 'a');} else {return 0;}}return current.getCount();}}class Node {private Node[] children;private int count;public Node(Node[] children, int count) {this.children = children;this.count = count;}public Node getChildrenItem(int i) {return children[i];}public void setChildrenItem(int i, Node node) {children[i] = node;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}}
程序二 HashMap类
import java.util.HashMap;import java.util.Map;import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner scn = new Scanner(System.in);Map<String, Integer> map = new HashMap<String, Integer>();while (scn.hasNext()) {String word = scn.nextLine();if (0 == word.length()) {break;}for (int i = 1; i <= word.length(); ++i) {String substr = word.substring(0, i);if (map.containsKey(substr)) {map.put(substr, map.get(substr) + 1);} else {map.put(substr, 1);}}}while (scn.hasNext()) {String word = scn.nextLine();if (map.containsKey(word)) {System.out.println(map.get(word));} else {System.out.println("0");}}}}
0 0
- ACM HDOJ 1251 (统计难题 )
- HDOJ 1251 统计难题
- 统计难题 HDOJ --1251
- hdoj 1251 统计难题
- Hdoj 1251 统计难题
- ACM--map+string--HDOJ 1251--统计难题--水
- HDOJ 1251 -- 统计难题 Trie
- Hdoj 1251 统计难题 【Hash】
- hdoj-1251-统计难题【map】
- hdu acm 1251 统计难题
- HDOJ 1251 统计难题 (trie)
- HDOJ 1251 统计难题 字典树
- [HDOJ 1251] 统计难题 [字典树]
- HDOJ-1251-统计难题 解题报告
- HDOJ 1251 统计难题 字典树
- hdoj 1251 统计难题 【字典树】
- HDOJ 1251 统计难题(字典树)
- HDOJ 统计难题 1251 【字典树】
- 英雄会(csdn pongo)题解之平衡二叉树——C++源代码
- POJ 2406 Power Strings KMP 求最小循环节
- Length of Last Word 最后一个单词的长度
- Android开发--内容提供者(Content provider)
- LeetCode OJ:Linked List Cycle
- ACM HDOJ 1251 (统计难题 )
- 【每天一个Linux命令】1. ln -s 软链接
- 4.在二元树中找出和为某一值的所有路径(树)
- poj2823
- # winform listView鼠标拖拽到pictureBox问题的解决办法,鼠标拖动
- myeclipse配置hadoop开发环境,及遇到问题解决
- hdu 2844题解
- LeetCode OJ:Word Break
- POJ 1125 Stockbroker Grapevine