计蒜客 Prefix Free Code(字典树+树状数组)
Consider n initial strings of lower case letters, where no initial string is a prefix of any other initial string. Now, consider choosing k of the strings (no string more than once), and concatenating them together. You can make this many such composite strings:
n×(n−1)×(n−2)×…×(n−k+1)
Consider sorting all of the composite strings you can get via this process in alphabetical order. You are given a test composite string, which is guaranteed to belong on this list. Find the position of this test composite string in the alphabetized list of all composite strings, modulo 10^9 + 7. The first composite string in the list is at position 1.
Input Format
Each input will consist of a single test case.
Note that your program may be run multiple times on different inputs.
Each test case will begin with a line with two integers, first nn and then k(1≤k≤n), where n is the number of initial strings, and k is the number of initial strings you choose to form composite strings. The upper bounds of nnand k are limited by the constraints on the strings, in the following paragraphs.
Each of the next n lines will contain a string, which will consist of one or more lower case letters a..z. These are the n initial strings. It is guaranteed that none of the initial strings will be a prefix of any other of the initial strings.
Finally, the last line will contain another string, consisting of only lower case letters a..z. This is the test composite string, the position of which in the sorted list you must find. This test composite string is guaranteed to be a concatenation of k unique initial strings.
The sum of the lengths of all input strings, including the test string, will not exceed 10^6 letters.
Output Format
Output a single integer, which is the position in the list of sorted composite strings where the test composite string occurs. Output this number modulo 10^9 + 7.
样例输入1
5 3 a b c d e cad
样例输出1
26
样例输入2
8 8 font lewin darko deon vanb johnb chuckr tgr deonjohnbdarkotgrvanbchuckrfontlewin
样例输出2
12451
题意
n个串任取k个,n个串互不为前缀,排序后,查询字符串所在的排名。
题解
可以知道查询的字符串唯一组成,假设为s1s2s3....sk,s1的排名为rk1。
那么答案ans=rk1*A(n-1,k-1)+rk2*A(n-2,k-2)+...+rkk*A(n-k,0)。
那么rk可以通过字典树知道,它真正的排名还需要减掉前面出现过的,这个用树状数组维护。
最后组合数预处理一下就行了。
代码
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int N=1000005; 4 const int MD=1000000007; 5 struct node{ 6 int v; 7 node *nxt[26]; 8 node() 9 { 10 v=0; 11 for(int i=0;i<26;i++)nxt[i]=NULL; 12 } 13 }; 14 node *root; 15 void ins(string s,int pos) 16 { 17 node *pre=root,*now; 18 int l=s.size(); 19 for(int i=0;i<l;i++) 20 { 21 int id=s[i]-'a'; 22 now=pre->nxt[id]; 23 if(now==NULL) 24 { 25 now=new node; 26 pre->nxt[id]=now; 27 } 28 pre=now; 29 } 30 pre->v=pos; 31 } 32 vector<string> vec; 33 string s,ss; 34 int a[N],n,k,f[N],inv[N]; 35 void add(int p,int x) { 36 while(p<=n) 37 a[p]=a[p]+x,p+=(p&-p); 38 } 39 int query(int p) { 40 int ans=0; 41 while(p>0) 42 ans=ans+a[p],p-=(p&-p); 43 return ans; 44 } 45 int quick_pow(int x,int y) { 46 int ans=1; 47 while(y) { 48 if(y&1) ans=1LL*ans*x%MD; 49 y>>=1; 50 x=1LL*x*x%MD; 51 } 52 return ans; 53 } 54 void init() { 55 f[0]=1; 56 for(int i=1;i<=n;i++) f[i]=1LL*f[i-1]*i%MD; 57 inv[n]=quick_pow(f[n],MD-2); 58 for(int i=n-1;i>=0;i--) inv[i]=1LL*inv[i+1]*(i+1)%MD; 59 } 60 int main() { 61 ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); 62 root=new node; 63 cin>>n>>k,init(); 64 for(int i=0;i<n;i++) cin>>s,vec.push_back(s); 65 sort(vec.begin(),vec.end()); 66 for(int i=0;i<n;i++) ins(vec[i],i+1); 67 for(int i=0;i<n;i++) add(i+1,1); 68 cin>>s; 69 int ans=1,t,p=0; 70 node *pre=root,*now; 71 for(int i=0;s[i];i++) { 72 int id=s[i]-'a'; 73 now=pre->nxt[id]; 74 pre=now; 75 if(now->v>0) 76 { 77 t=now->v; 78 add(t,-1); 79 p++; 80 ans=(ans+1LL*query(t)*f[n-p]%MD*inv[n-k]%MD)%MD; 81 pre=root; 82 } 83 } 84 cout<<ans<<'\n'; 85 return 0; 86 }