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可以通过字典树知道,它真正的排名还需要减掉前面出现过的,这个用树状数组维护。

最后组合数预处理一下就行了。

代码

 #include <bits/stdc++.h>
using namespace std;
const int N=;
const int MD=;
struct node{
int v;
node *nxt[];
node()
{
v=;
for(int i=;i<;i++)nxt[i]=NULL;
}
};
node *root;
void ins(string s,int pos)
{
node *pre=root,*now;
int l=s.size();
for(int i=;i<l;i++)
{
int id=s[i]-'a';
now=pre->nxt[id];
if(now==NULL)
{
now=new node;
pre->nxt[id]=now;
}
pre=now;
}
pre->v=pos;
}
vector<string> vec;
string s,ss;
int a[N],n,k,f[N],inv[N];
void add(int p,int x) {
while(p<=n)
a[p]=a[p]+x,p+=(p&-p);
}
int query(int p) {
int ans=;
while(p>)
ans=ans+a[p],p-=(p&-p);
return ans;
}
int quick_pow(int x,int y) {
int ans=;
while(y) {
if(y&) ans=1LL*ans*x%MD;
y>>=;
x=1LL*x*x%MD;
}
return ans;
}
void init() {
f[]=;
for(int i=;i<=n;i++) f[i]=1LL*f[i-]*i%MD;
inv[n]=quick_pow(f[n],MD-);
for(int i=n-;i>=;i--) inv[i]=1LL*inv[i+]*(i+)%MD;
}
int main() {
ios::sync_with_stdio(false),cin.tie(),cout.tie();
root=new node;
cin>>n>>k,init();
for(int i=;i<n;i++) cin>>s,vec.push_back(s);
sort(vec.begin(),vec.end());
for(int i=;i<n;i++) ins(vec[i],i+);
for(int i=;i<n;i++) add(i+,);
cin>>s;
int ans=,t,p=;
node *pre=root,*now;
for(int i=;s[i];i++) {
int id=s[i]-'a';
now=pre->nxt[id];
pre=now;
if(now->v>)
{
t=now->v;
add(t,-);
p++;
ans=(ans+1LL*query(t)*f[n-p]%MD*inv[n-k]%MD)%MD;
pre=root;
}
}
cout<<ans<<'\n';
return ;
}

最新文章

  1. nuget常用命令
  2. kaungbin_DP S (POJ 3666) Making the Grade
  3. Web环境使用相对路径发布Webservice
  4. Java--笔记(4)
  5. C# winform应用程序仅能打开一个进程运行
  6. list2json
  7. 去除DedeCms 5.7后台版权广告链接的方法
  8. redis Ok2
  9. 【转】我的电脑最近忽然开不了机,启动修复也无法修复,win7系统。开机的时候如果不点启动修复直接正常启动
  10. 数据结构(主席树):HDU 4729 An Easy Problem for Elfness
  11. JS中setAttribute的兼容性问题(摘自leejersey)
  12. POJ1006: 中国剩余定理的完美演绎(非原创)
  13. 搭建LAMP环境示例
  14. CentOS 7 使用iptables防火墙
  15. html获取输入框的值
  16. Improved GAN
  17. js的&quot;|&quot;
  18. gattAttribute_t 含义 中文解释
  19. JAVA基础之sql模糊匹配、外键以及jsp中include的用法
  20. Windows如何设置动态和静态ip地址

热门文章

  1. docker 安装 ElasticSearch
  2. Js中的onblur和onfocus事件应用介绍
  3. Elasticsearch template学习
  4. ES6 学习笔记(基础)
  5. Python学习day11-函数基础(1)
  6. Activiti流程变量
  7. 大批量数据导出excel
  8. 2019-8-31-dotnet-如何在-Mock-模拟-Func-判断调用次数
  9. Laravel 指定日志生成目录
  10. [转]WPF中的ControlTemplate(控件模板)