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

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <string>
#include <deque>
using namespace std;
#define ll long long
#define N 1000009
#define gep(i,a,b) for(int i=a;i<=b;i++)
#define gepp(i,a,b) for(int i=a;i>=b;i--)
#define gep1(i,a,b) for(ll i=a;i<=b;i++)
#define gepp1(i,a,b) for(ll i=a;i>=b;i--)
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
#define lowbit(x) x&(-x)
ll n,m;
ll pos,dfn;
ll tree[N][],c[N],a[N];
char s[N];
ll vis[N];
ll fac[N] = {, }, inv[N] = {, }, f[N] = {, };
void init(){
gep(i,,N){
fac[i]=fac[i-]*i%mod;
f[i]=(mod-mod/i)*f[mod%i]%mod;
inv[i]=inv[i-]*f[i]%mod;
}
}
ll A(ll n,ll m){
if(n<m) return ;
return fac[n]*inv[n-m]%mod;//一开始*写成了%
}
void update(ll i,ll num){
while(i<=n){
c[i]+=num;
i+=lowbit(i);
}
}
ll getsum(ll i){
ll sum=;
while(i>){
sum+=c[i];
i-=lowbit(i);
}
return sum;
}
void dfs(int u){
if(vis[u]) vis[u]=++dfn;//排序
gep(i,,){
if(tree[u][i]) dfs(tree[u][i]);
}
}
int main()
{
init();
scanf("%lld%lld",&n,&m);
pos=;
gep1(i,,n){
scanf("%s",s);
ll l=strlen(s)-;
ll x=;
//建字典树
gep1(j,,l){
if(!tree[x][s[j]-'a']) tree[x][s[j]-'a']=++pos;
x=tree[x][s[j]-'a'];
}
vis[x]=;//只标记最后的元素
}
dfn=;
dfs();
scanf("%s",s);
ll l=strlen(s)-;
ll x=,cnt=;
gep1(i,,l){
x=tree[x][s[i]-'a'];
if(vis[x]) a[++cnt]=vis[x],x=;//找到每个的标记,每次还要x==0
}
gep1(i,,n) update(i,);
ll ans=;
gep1(i,,cnt){
update(a[i],-);
ll ans1=getsum(a[i]);//前面还可以再用的
ll ans2=A(n-i,m-i);
ans=(ans+ans1*ans2%mod)%mod;
}
printf("%lld\n",ans);
return ;
}

最新文章

  1. 渡轮问题Ship
  2. JQuery基础一
  3. 开启xp_cmdshell
  4. Effective C++笔记04:设计与声明
  5. Angular - - $compile编译服务与指令
  6. JavaScript基础:BOM的常见内置方法和内置对象
  7. ASP.NET MVC系列:web.config中ConnectionString aspnet_iis加密与AppSettings独立文件
  8. Docker五大优势:持续集成、版本控制、可移植性、隔离性和安全性
  9. 【消灭代办】第4周 - Echarts在移动端的各种填坑姿势
  10. 计蒜客---N的-2进制表示
  11. python邮件发送
  12. 【LOJ】#2115. 「HNOI2015」落忆枫音
  13. mysql 8.0给数据库添加用户和赋权
  14. adb server version (31) doesn&#39;t match this client (36)
  15. Haskell语言学习笔记(57)Parsec(4)
  16. 三、Linux学习之命令基本格式篇
  17. Android笔记(十)ListView
  18. iOS 内存泄漏监测自动化
  19. 在vscode中使用webpack中安装的echarts文件失败,dom获取class名,图表不显示
  20. 通过windows的超级终端连接华为交换机

热门文章

  1. Serega and Fun Codeforces - 455D || queue
  2. 利用Common-Fileupload上传文件图片
  3. myBatis-类型关联
  4. aspectj xml
  5. 学习php中的mysql()函数
  6. GCD 使用说明
  7. 基于H5+ API手机相册图片压缩上传
  8. [windows]设置开机取消登录窗口选项直接进入桌面
  9. PHP 常用的无限遍历方法
  10. 【Web应用-网络连接】Azure Web 应用对外连接数上限分析