• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
ACM s1124yy
守りたいものが 強くさせること
博客园    首页    新随笔    联系   管理     

HDU 1251 统计难题(Trie)

统计难题

【题目链接】统计难题

【题目类型】Trie

&题解:

Trie的模板题,只不过这题坑点在没给数据范围,改成5e5就可以过了,用的刘汝佳蓝书模板

&代码:

#include <cstdio>
#include <bitset>
#include <iostream>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;
const int maxNode= 500000 +7,sigmaSize=26;
struct Trie
{
    int ch[maxNode][sigmaSize],sz,val[maxNode];
    Trie(){sz=1; memset(ch[0],0,sizeof(ch[0])); memset(val,0,sizeof(val));}
    int idx(char c){return c-'a';}
    void insert(char* s)
    {
        int u=0,n=strlen(s);
        for(int i=0;i<n;i++){
            int c=idx(s[i]);
            if(!ch[u][c]){
                memset(ch[sz],0,sizeof(ch[sz]));
                ch[u][c]=sz++;
            }
            u=ch[u][c];
            val[u]++;
        }
    }
    int query(char* s)
    {
        int u=0,n=strlen(s);
        for(int i=0;i<n;i++){
            int c=idx(s[i]);
            if(!ch[u][c]){
                return 0;
            }
            u=ch[u][c];
        }
        return val[u];
    }
}tr;
char s[23];
int main()
{
    while(1){
        gets(s);
        int n=strlen(s);
        if(n==0) break;
        tr.insert(s);
    }
    while(gets(s)){
        printf("%d\n",tr.query(s));
    }
    return 0;
}
posted @ 2017-04-12 13:35  s1124yy  阅读(165)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3