zoukankan      html  css  js  c++  java
  • PFS集合


    有一种特殊的集合叫做PFS(Prefix Free Set)集合。
    一个PFS集合由若干字符串构成,且不存在一个字符串是另一个字符串的前缀。空集也被看作是PFS集合。
    例如 {"hellohello"} 和 {"hellohello", "goodbyegoodbye", "giantgiant", "hihi"} 是PFS集合,但 {"hellohello","hellhell"} 和{"greatgreat","giggig","gg"} 不是。
    现在给你一个集合,请你求出它的子集是PFS集合的子集个数,答案对9191取模。
    Input
    输入数据第一行一个整数n,表示集合里元素的个数。
    以下n行,每行一个非空字符串s,仅包含小写英文字母,表示集合中的元素。数据保证不存在两个相同的字符串。
    1≤n≤50000,字符串长度不大于50
    Output
    输出一个正整数ans,表示对9191取模后的答案。
    Sample Input
    3
    hello
    hi
    hell
    Sample Output
    6
    HINT
    //除了 {"hellhell","hellohello"} 和 {"hihi","hellohello","hellhell"} 两种情况外,其余情况均是PFS集合

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<iostream>
    #include<cctype>
    using namespace std;
    inline void read(int& x)
    {
    	char c = getchar();x = 0;
    	while(!isdigit(c))c=getchar();
    	while(isdigit(c)){x=x*10+c-'0';c=getchar();}
    }
    int n,tot;char s[55];
    int trie[1100000][27];
    int v[1100000];
    void insert()
    {
    	int u = 0;
    	int len = strlen(s);
    	for(int i=0;i<len;i++)
    	{
    		int c = s[i]-'a'+1;
    		if(!trie[u][c])
    			trie[u][c]=++tot;
    		u = trie[u][c];
    	}
    	v[u] = 1;	
    }
    int dfs(int u)
    {
    	int sum = 1;
    	for(int i=1;i<=26;i++)
    	{
    		if(trie[u][i])
    		{
    			int k = dfs(trie[u][i]);
    			sum*=k;
    			sum%=9191;
    		}
    	}
    	v[u]+=sum;
    	v[u]%=9191;
    	return v[u];
    }
    int main(void)
    {
    	read(n);
    	for(int i=1;i<=n;i++){gets(s);insert();}
    	printf("%d",dfs(0));
    	return 0;
    }
    

      

  • 相关阅读:
    MongoClient类
    MongoDB基本操作(增删改查)
    为什么MongoDB适合大数据的存储?
    npm的使用
    nodejs安装教程
    c#byte数组和string类型转换
    C#中数组、ArrayList和List三者的区别
    eclspse魔板
    mysql的备份
    shell的使用
  • 原文地址:https://www.cnblogs.com/cutemush/p/12601736.html
Copyright © 2011-2022 走看看