The North American Invitational Programming Contest 2018 E. Prefix Free Code
Consider nn initial strings of lower case letters, where no initial string is a prefix of any other initial string. Now, consider choosing kk of the strings (no string more than once), and concatenating them together. You can make this many such composite strings:
\displaystyle n \times (n - 1) \times (n - 2) \times . . . \times (n - k + 1)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 + 7109+7. The first composite string in the list is at position 11.
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 \le k \le n)k(1≤k≤n), where nn is the number of initial strings, and kk is the number of initial strings you choose to form composite strings. The upper bounds of nnand kk are limited by the constraints on the strings, in the following paragraphs.
Each of the next nn lines will contain a string, which will consist of one or more lower case letters a..za..z. These are the nn 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..za..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 kk unique initial strings.
The sum of the lengths of all input strings, including the test string, will not exceed 10^6106 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 + 7109+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
题目来源
The North American Invitational Programming Contest 2018
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstdlib> 5 #include <cstring> 6 #include <string> 7 #include <deque> 8 using namespace std; 9 #define ll long long 10 #define N 1000009 11 #define gep(i,a,b) for(int i=a;i<=b;i++) 12 #define gepp(i,a,b) for(int i=a;i>=b;i--) 13 #define gep1(i,a,b) for(ll i=a;i<=b;i++) 14 #define gepp1(i,a,b) for(ll i=a;i>=b;i--) 15 #define mem(a,b) memset(a,b,sizeof(a)) 16 #define mod 1000000007 17 #define lowbit(x) x&(-x) 18 ll n,m; 19 ll pos,dfn; 20 ll tree[N][30],c[N],a[N]; 21 char s[N]; 22 ll vis[N]; 23 ll fac[N] = {1, 1}, inv[N] = {1, 1}, f[N] = {1, 1}; 24 void init(){ 25 gep(i,2,N){ 26 fac[i]=fac[i-1]*i%mod; 27 f[i]=(mod-mod/i)*f[mod%i]%mod; 28 inv[i]=inv[i-1]*f[i]%mod; 29 } 30 } 31 ll A(ll n,ll m){ 32 if(n<m) return 0; 33 return fac[n]*inv[n-m]%mod;//一开始*写成了% 34 } 35 void update(ll i,ll num){ 36 while(i<=n){ 37 c[i]+=num; 38 i+=lowbit(i); 39 } 40 } 41 ll getsum(ll i){ 42 ll sum=0; 43 while(i>0){ 44 sum+=c[i]; 45 i-=lowbit(i); 46 } 47 return sum; 48 } 49 void dfs(int u){ 50 if(vis[u]) vis[u]=++dfn;//排序 51 gep(i,0,25){ 52 if(tree[u][i]) dfs(tree[u][i]); 53 } 54 } 55 int main() 56 { 57 init(); 58 scanf("%lld%lld",&n,&m); 59 pos=0; 60 gep1(i,1,n){ 61 scanf("%s",s); 62 ll l=strlen(s)-1; 63 ll x=0; 64 //建字典树 65 gep1(j,0,l){ 66 if(!tree[x][s[j]-'a']) tree[x][s[j]-'a']=++pos; 67 x=tree[x][s[j]-'a']; 68 } 69 vis[x]=1;//只标记最后的元素 70 } 71 dfn=0; 72 dfs(0); 73 scanf("%s",s); 74 ll l=strlen(s)-1; 75 ll x=0,cnt=0; 76 gep1(i,0,l){ 77 x=tree[x][s[i]-'a']; 78 if(vis[x]) a[++cnt]=vis[x],x=0;//找到每个的标记,每次还要x==0 79 } 80 gep1(i,1,n) update(i,1); 81 ll ans=1; 82 gep1(i,1,cnt){ 83 update(a[i],-1); 84 ll ans1=getsum(a[i]);//前面还可以再用的 85 ll ans2=A(n-i,m-i); 86 ans=(ans+ans1*ans2%mod)%mod; 87 } 88 printf("%lld\n",ans); 89 return 0; 90 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现