• UVA5913 Dictionary Sizes(字典树)(转载)


    题目大意:给出n个旧单词,要从这n个旧单词中构造新单词。构造条件是 S = Sa + Sb,其中Sa为某个旧单词的非空前缀,Sb为某个单词的非空后缀。求所有的新单词和旧单词中有多少个不同的单词。

    思路:将所有单词建成一棵字典树,再将所有单词反转并建成一棵字典树。则第一棵树的结点个数即为不同前缀的数量,第二棵树的结点个数为不同后缀的数量。如果不算重复,取两者的乘积即为新单词的数量。

               接下来考虑重复的情况。什么样子会重复呢?假设第一棵树中某个前缀为a1a2a3...X,第二棵树中某个后缀为Xb1b2b3...。那么这就一定会有重复,也就是前缀的末字符和后缀的首字符相同时会重复。考虑a1a2a3...Xb1b2b3...,这个X可能出现在前缀里,也可能出现在后缀里,这也就是多算了一种情况。考虑一般情况,假设第一棵树中某个前缀为a1a2a3...XXX..X(一共m个X),第二棵树中某个后缀为XXX...Xb1b2b3...(一共n个X),那么这两个串组成的串a1a2a3XX...Xb1b2b3...中X的数目有m+n+1种可能(0~m+n),而前缀中X的数目有(m+1)种可能(0~m个),同理后缀中X的数目(n+1)种可能,因此我们在实际计算中一共多算了(m+1)*(n+1) - (m+n+1) = m*n次,即前缀中X的数目与后缀中X数目的乘积。

               那么,我们只要分别求出两棵树中每个字符的出现次数并减去它们的乘积即可(这是用了加法乘法原理,假设第一棵树中末尾为X的前缀共有3个,分别含a1、a2、a3个X;第二棵树中开头为X的后缀为3个,分别含b1、b2、b3个X,则一共重复算了a1*b1+a1*b2+a1*b3+a2*b1+a2*b2+a2*b3+a3*b1+a3*b2+a3*b3=(a1+a2+a3)*(b1+b2+b3)次)。这里要注意,只统计深度大于1的结点,因为我们要保证前后缀均非空(如前缀X与后缀Xb就不会有重复,因为第一个的X必须要选)。最后,再加上长度为1的旧单词即可,因为我们构造单词时不会造出长度为1的单词。

    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cctype>
    #include<iostream>
    #include<set>
    #include<map>
    #include<cmath>
    #include<sstream>
    #include<vector>
    #include<stack>
    #include<queue>
    #include<algorithm>
    #define fin freopen("a.txt","r",stdin)
    #define fout freopen("a.txt","w",stdout)
    typedef long long LL;
    typedef unsigned long long ULL;
    using namespace std;
    const int inf = 1e9 + 10;
    const int maxnode = 4e5 + 10;
    const int sigma_size = 26;
    const int maxn = 10000 + 10;
    char s[45];
    int vis[sigma_size];
     
    struct Tree
    {
        int ch[maxnode][sigma_size];
        int val[maxnode];
        int cnt[sigma_size];
        int sz;
        int idx(char c) { return c - 'a'; }
        void init() { memset(ch[0], 0, sizeof ch[0]); sz = 1; memset(cnt, 0, sizeof cnt); }
     
        void insert(char *s)
        {
            int n = strlen(s), u = 0;
            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++;
                    if(i) cnt[c]++;
                }
                u = ch[u][c];
                
            }
            val[u] = 1;
        }
     
    }Pre, Suf;
     
    int main()
    {
     
        int n;
        while(scanf("%d", &n) == 1)
        {
            Pre.init(); Suf.init();
            memset(vis, 0, sizeof vis);
            for(int i = 1; i <= n; i++)
            {
                scanf("%s", s);
                Pre.insert(s);
                int len = strlen(s);
                reverse(s, s+len);
                Suf.insert(s);
                if(len == 1) vis[s[0]-'a'] = 1;
            }
            LL ans = LL(Pre.sz-1)*LL(Suf.sz-1);
            for(int i = 0; i < sigma_size; i++)
                ans -= (LL)Pre.cnt[i] * LL(Suf.cnt[i]);
            for(int i = 0; i < sigma_size; i++)
                if(vis[i]) ++ans;
            cout << ans << endl;
        }
        return 0;
    }
  • 相关阅读:
    Understanding identities in IIS
    Name your feature branches by convention
    Branch policies on Azure Repos
    Use Git Credential Managers to Authenticate to Azure Repos
    How do I force my .NET application to run as administrator?
    UML的类型
    ASP.NET Error Handling
    通过泛型,将string转换为指定类型
    Spring Session + Redis实现分布式Session共享
    MongoDB中的数据导出为excel CSV 文件
  • 原文地址:https://www.cnblogs.com/wrjlinkkkkkk/p/9527092.html
Copyright © 2020-2023  润新知