zoukankan      html  css  js  c++  java
  • 【统计难题】【HDU

    思路

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

    AC代码

    代码1:map打表

    #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] == '')
                break;
            string ss = "";
            for(int i = 0; i < s.size(); i++)
            {
                ss += s[i];
                table[ss]++;
            }
        }
        while(cin >> s)
        {
            cout << table[s] << endl;
        }
    }
    

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

    #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] == '')
                break;
            inserts(s);
        }
        while(scanf("%s", s) != EOF)
        {
            cout << searchs(s) << endl;
        }
    }
    

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

    //#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] == '')
                break;
            inserts(s);
        }
        while(scanf("%s", s) != EOF)
        {
            cout << finds(s) << endl;
        }
    }
    
  • 相关阅读:
    常见业务指标
    1006 换个格式输出整数 (Python)
    1004 成绩排名 (Python)
    1003 我要通过! (Python)
    1008 数组元素循环右移问题 (Python)
    如何使用SSH秘钥链接Github
    在windows下如何正确安装curses模块
    面向数据结构C基础知识点(个人向)
    用Python实现链式调用
    python重点串讲
  • 原文地址:https://www.cnblogs.com/KeepZ/p/11370931.html
Copyright © 2011-2022 走看看