hihoCoder #1014 : Trie树
来源:互联网 发布:武汉软件新城派出所 编辑:程序博客网 时间:2024/06/04 19:06
hihoCoder #1014 : Trie树
小白做题系列。
原题传送门
Code
//// main.cpp// Algorithm_OJ2//// Created by PTkin_Wang on 16/3/25.// Copyright © 2016年 PTkin_Wang. All rights reserved.//#include <iostream>#include <string.h>using namespace std;class Node {public : int count; char letter; bool exist_sub_node[26]; Node *sub_node[26]; Node() { count = 0 ; letter = '0'; memset(exist_sub_node, 0, 26); memset(sub_node, 0, 26); } void insert(string str) { ++count; if (0 == str.length()) return; int index = str[0] - 'a'; if (!exist_sub_node[index]) { // not exist Node *nextNode = new Node(); nextNode->letter = str[0]; exist_sub_node[index] = true; sub_node[index] = nextNode; } sub_node[index]->insert(str.substr(1, str.size() - 1)); }};void check(string str, Node *node) { if (0 == str.length()) { cout << node->count; return; } int index = str[0] - 'a'; if (!node->exist_sub_node[index]) { cout << '0'; } else { check(str.substr(1, str.size() - 1), node->sub_node[index]); }}int main(int argc, const char * argv[]) { Node *root = new Node(); int input_num; string input_word; cin >> input_num; // number of words in the dictionary for (int i = 0; i < input_num; ++i) { cin >> input_word; root->insert(input_word); } cin >> input_num; // number of words to be checked for (int i = 0; i < input_num; ++i) { cin >> input_word; check(input_word, root); if (i != input_num - 1) cout << endl; } return 0;}
Note
要得到 string
类型的长度应该使用 .size()
而不是 sizeof()
太久没写 C++ 都不熟悉了,误以为 sizeof(string)
可以得到字符串的长度,实际上如果要得到 string
的长度应该使用 string.size()
:)。
string
的本质是一个类,调用对象方法可以得到它的字符串长度,而使用 sizeof(string)
只能得到这个对象的大小,里面实际存储的包括了一个指向堆内的字符串的指针。
尝试一下下面代码:
#include <iostream>using namespace std;void output(string str) { cout << "str = \"" << str << "\"" << endl << "sizeof(\"" << str << "\") = " << sizeof(str) << endl << "\"" << str << "\".size() = " << str.size() << "\n\n";}int main(int argc, const char * argv[]) { string str1 = "ddd"; string str2 = "asudfghasodhsdf"; string str3 = "asudfghasodhfauosdfalsdfasudfgodhfauosdfa"; output(str1); output(str2); output(str3); return 0;}
将会输出
str = "ddd"sizeof("ddd") = 24"ddd".size() = 3str = "asudfghasodhsdf"sizeof("asudfghasodhsdf") = 24"asudfghasodhsdf".size() = 15str = "asudfghasodhfauosdfalsdfasudfgodhfauosdfa"sizeof("asudfghasodhfauosdfalsdfasudfgodhfauosdfa") = 24"asudfghasodhfauosdfalsdfasudfgodhfauosdfa".size() = 41
0 0
- hihocoder 1014 Trie树 trie
- [hihoCoder 1014][Trie 树]Trie
- hihocoder 1014 trie树
- hihocoder#1014 : Trie树
- hihoCoder #1014 : Trie树
- [HiHoCoder]#1014 : Trie树
- hihoCoder#1014Trie树
- hihoCoder #1014 Trie树
- hihocoder-#1014 : Trie树
- hihoCoder 1014 : Trie树
- hihoCoder #1014 : Trie树
- hihocoder-#1014 : Trie树
- hihocoder #1014 trie 树
- hihoCoder - 1014 - Trie树
- hihoCoder 1014 Trie树
- hihocoder 1014 trie树
- HihoCoder 1014 : Trie树
- hihoCoder 1014Trie树
- mysql中<>与null值不能比较
- 设计模式之观察者模式
- 3.Utm详细实现-用户生命流程
- PAT1029旧键盘(20)
- Python lamda
- hihoCoder #1014 : Trie树
- C语言完成2048,辣鸡代码(#滑稽#)
- Link Cut Tree(LCT )学习笔记
- 选择问题-第k小元素
- #java读书笔记#面向对象1
- 【机房重构个人版】存储过程的使用
- 深度懵逼计算机系统。。。DataLab。。
- ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2
- BZOJ 1199: [HNOI2005]汤姆的游戏