前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【统计难题】【HDU - 1251】【map打表或字典树】【字典树模板】

【统计难题】【HDU - 1251】【map打表或字典树】【字典树模板】

作者头像
_DIY
发布2019-08-23 19:37:30
4270
发布2019-08-23 19:37:30
举报

思路

题意题目为中文题,这里不再过多阐述。 思路1:可以在读入单词表的过程中将单词分解,用map将它一 一记录 思路2:利用字典树,这个方法较快些,下面代码中会分别给出数组和结构体指针两种形式的字典树,指针形式的有时可能会因题目内存限制而导致Memory Limit Exceeded,这时就可选择数组形式的。不过对于本题来说不用担心。

AC代码

代码1:map打表

代码语言:javascript
复制
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
map<string, int> table;
int main()
{
    std::ios::sync_with_stdio(false);
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    table.clear();
    string s;
    while(getline(cin, s))
    {
        if(s[0] == '\0')
            break;
        string ss = "";
        for(int i = 0; i < s.size(); i++)
        {
            ss += s[i];
            table[ss]++;
        }
    }
    while(cin >> s)
    {
        cout << table[s] << endl;
    }
}

代码2:数组形式的字典树

代码语言:javascript
复制
#include<bits/stdc++.h>
using namespace std;
const int maxnode = 400001;
const int maxs = 27;
char s[10 + 10];
int trie[maxnode][maxs] ;
int sum[maxnode] ;
int node = 0;
void inserts(char *t)
{
    int len = strlen(t);
    int cur = 0;
    for(int i = 0; i < len; i++)
    {
        int p = t[i] - 'a';
        if(!trie[cur][p])
            trie[cur][p] = ++node;
        sum[trie[cur][p]]++;
        cur = trie[cur][p];
    }
}
int searchs(char *t)
{
    int len = strlen(t);
    int cur = 0;
    for(int i = 0; i < len; i++)
    {
        int p = t[i] - 'a';
        if(!trie[cur][p])
            return 0;
        cur = trie[cur][p];
    }
    return sum[cur];
}
int main()
{
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    memset(trie, 0, sizeof(trie));
    memset(sum, 0, sizeof(sum));
    while(gets(s) != NULL)
    {
        if(s[0] == '\0')
            break;
        inserts(s);
    }
    while(scanf("%s", s) != EOF)
    {
        cout << searchs(s) << endl;
    }
}

代码3:结构体指针形式的字典树

代码语言:javascript
复制
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char s[10+10];
struct Trie
{
    Trie* next[26];
    int sum;
    Trie()
    {
        for(int i = 0; i < 26; i++)
            next[i] = NULL;
        sum = 0;
    }
}root;
void inserts(char *s)
{
    Trie* p = &root;
    int len = strlen(s);
    for(int i = 0; i < len; i++)
    {
        int cur = s[i] - 'a';
        if(p->next[cur] == NULL)
        {
            p->next[cur] = new Trie;
        }
        p = p->next[cur];
        p->sum++;
    }
}
int finds(char *s)
{
    Trie* p = &root;
    int len = strlen(s);
    for(int i = 0; i < len; i++)
    {
        int cur = s[i] - 'a';
        if(p->next[cur] == NULL)
            return 0;
        else
            p = p->next[cur];
    }
    return p->sum;
}


int main()
{
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    while(gets(s) != NULL)
    {
        if(s[0] == '\0')
            break;
        inserts(s);
    }
    while(scanf("%s", s) != EOF)
    {
        cout << finds(s) << endl;
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 思路
  • AC代码
    • 代码1:map打表
      • 代码2:数组形式的字典树
        • 代码3:结构体指针形式的字典树
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档