标签:new 入门 字典树 abs 字符串 输入数据 panel cout input
banana band bee absolute acm ba b band abcSample Output
2 3 1 0
字典树入门题,提交g++,总是超空间,提交c++就对了。。倒是不难,却搞得我一直在想办法。。
代码:
#include <iostream> #include <cstdlib> #include <string> using namespace std; struct DTree { int c; DTree *next[26]; }; DTree *CreatDTree() { DTree *head = new DTree; head -> c = 0; for(int i = 0;i < 26;i ++) { head -> next[i] = NULL; } return head; } DTree *SetDTree(DTree *head,string a) { DTree *h = head; for(int i = 0;i < a.size();i ++) { if(head -> next[a[i] - ‘a‘] == NULL) { head -> next[a[i] - ‘a‘] = CreatDTree(); } head = head -> next[a[i] - ‘a‘]; head -> c ++; } return h; } int Count(DTree *head,string b) { for(int i = 0;i < b.size();i ++) { head = head -> next[b[i] - ‘a‘]; if(head == NULL) return 0; } return head -> c; } int main() { string a,b; DTree *head = CreatDTree(); while(getline(cin,a)&&a.size()) { head = SetDTree(head,a); } while(cin>>b) { cout<<Count(head,b)<<endl; } }
标签:new 入门 字典树 abs 字符串 输入数据 panel cout input
原文地址:http://www.cnblogs.com/8023spz/p/7768876.html