• 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 imes (n - 1) imes (n - 2) imes . . . imes (n - k + 1)n×(n1)×(n2)×...×(nk+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(1kn), 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
    ",ans);
    89     return 0;
    90 }
  • 相关阅读:
    前端性能优化方法
    软件测试十大原则
    CSRF攻击【转载】
    XSS攻击(出现的原因、预防措施......)
    python 连接MySQL数据库
    《快消品营销人的第一本书》
    python 格式化输出日志记录
    python 安装cv2
    测试用例设计的策略
    【转】APP功能测试要领
  • 原文地址:https://www.cnblogs.com/tingtin/p/9483858.html
Copyright © 2020-2023  润新知