hdu 1251统计难题
来源:互联网 发布:网络主播管理制度 编辑:程序博客网 时间:2024/05/17 08:37
做了几道Trie了,这个题做过之后,让我对Trie又有的许多新的认识,真正的理解了插入和查询的整个过程。
// File Name: hdu1251.cpp// Author: Toy// Created Time: 2013年05月08日 星期三 09时17分28秒#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <cctype>#include <cmath>#include <string>#include <algorithm>#include <cstdlib>#include <iomanip>#include <list>#include <map>#include <set>#include <deque>#include <stack>#include <utility>#include <bitset>#define L(x) x << 1#define R(x) x << 1 | 1using namespace std;const int sigma_size = 26;const int maxnode = 10 * 50000 + 10;string s, tt;bool flag;struct Trie { int ch[maxnode][sigma_size]; int val[maxnode]; int sz; void clear ( ) { sz = 1; memset ( ch[0], 0, sizeof ( ch[0] ) ); } int idx ( char c ) { return c - 'a'; } void insert ( string s, int v ) {int u = 0, n = s.length ();for ( int i = 0; i < n; ++i ) { int c = idx ( s[i] ); if ( !ch[u][c] ) {memset ( ch[sz], 0, sizeof ( ch[sz] ) );val[sz] = 0;ch[u][c] = sz++; } val[u]++; u = ch[u][c];}val[u]++; } void find ( string s ) {flag = 1;int u = 0, n = s.length ();for ( int i = 0; i < n; ++i ) { if ( s[i] == '\0' ) {flag = 0;break; } int c = idx ( s[i] ); if ( !ch[u][c] ) {flag = 0; break; } u = ch[u][c];}if ( flag ) cout << val[u] << endl;else cout << "0" << endl; }}trie;int main ( ) { trie.clear(); while ( getline ( cin, s ) ) {if ( s != "" ) trie.insert ( s, 1 );else { while ( getline ( cin, tt ) ) {trie.find ( tt ); }} } return 0;}
- HDU 1251 统计难题
- HDU 1251 统计难题
- hdu 1251 统计难题
- HDU 1251:统计难题
- HDU 1251 统计难题
- Hdu-1251 统计难题
- hdu 1251 统计难题
- hdu 1251 统计难题
- hdu 1251 统计难题
- HDU-1251-统计难题
- HDU 1251 统计难题
- hdu 1251统计难题
- hdu 1251统计难题
- hdu 1251 统计难题
- HDU 1251 统计难题
- hdu 1251统计难题
- HDU 1251 统计难题
- hdu 1251 统计难题
- csdn赚积分方法
- 插入排序算法实现(直接插入排序和希尔排序)
- 浅析人脸检测之Haar分类器方法
- iOS录音功能的实现
- could not load file or assembly 'system.web.mvc version=1.0.0.0'
- hdu 1251统计难题
- 简单粗糙的指尖检测方法(FingerTips Detection)
- Mac OS图形界面演化历史
- C/C++ sizeof总结
- 机器学习知识点学习
- 和机器学习和计算机视觉相关的数学之一
- uva 10054 - The Necklace
- 经典排序算法综述 持续更新中
- 跳马问题(骑士周游问题)初探