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 ;
}

计蒜客 Prefix Free Code(字典树+树状数组)的更多相关文章

  1. 计蒜客 青出于蓝胜于蓝(dfs序+树状数组)

    题目描述 武当派一共有 n 人,门派内 n 人按照武功高低进行排名,武功最高的人排名第 1,次高的人排名第 2,... 武功最低的人排名 第 n.现在我们用武功的排名来给每个人标号,除了祖师爷,每个人 ...

  2. 计蒜客 31451 - Ka Chang - [DFS序+树状数组][2018ICPC沈阳网络预赛J题]

    题目链接:https://nanti.jisuanke.com/t/31451 Given a rooted tree ( the root is node $1$ ) of $N$ nodes. I ...

  3. 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)

    Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...

  4. 计蒜客16492 building(二分线段树/分块)

    题解: 考虑用线段树维护楼的最大值,然后这个问题就很简单了. 每次可以向左二分出比x高的第一个楼a,同理也可以向右二分出另一个楼b,如果a,b都存在,答案就是b-a-1. 注意到二分是可以直接在线段树 ...

  5. 计蒜客 28449.算个欧拉函数给大家助助兴-大数的因子个数 (HDU5649.DZY Loves Sorting) ( ACM训练联盟周赛 G)

    ACM训练联盟周赛 这一场有几个数据结构的题,但是自己太菜,不会树套树,带插入的区间第K小-替罪羊套函数式线段树, 先立个flag,BZOJ3065: 带插入区间K小值 计蒜客 Zeratul与Xor ...

  6. 爬虫acm比赛成绩(多页成绩整合在一起、获取复制不了的数据)(hihocoder、计蒜客)

    https://github.com/congmingyige/web-crawler_rank-of-competition-in-JiSuanKe-and-hihocoder 1. 计蒜客(获取复 ...

  7. 计蒜客 NOIP 提高组模拟竞赛第一试 补记

    计蒜客 NOIP 提高组模拟竞赛第一试 补记 A. 广场车神 题目大意: 一个\(n\times m(n,m\le2000)\)的网格,初始时位于左下角的\((1,1)\)处,终点在右上角的\((n, ...

  8. 计蒜客 A1607 UVALive 8512 [ACM-ICPC 2017 Asia Xi'an]XOR

    ICPC官网题面假的,要下载PDF,点了提交还找不到结果在哪看(我没找到),用VJ交还直接return 0;也能AC 计蒜客题面 这个好 Time limit 3000 ms OS Linux 题目来 ...

  9. [计蒜客] 矿石采集【记搜、Tarjan缩点+期望Dp】

    Online Judge:计蒜客信息学3月提高组模拟赛 Label:记搜,TarJan缩点,树状数组,期望Dp 题解 整个题目由毫无关联的两个问题组合成: part1 问题:对于每个询问的起点终点,求 ...

随机推荐

  1. pb_ds(平板电视)简介

    据说NOI赛制可以用pbds,故整理常用方法: 1.splay 所需声明及头文件: #include <ext/pb_ds/tree_policy.hpp> #include <ex ...

  2. Window和Mac下端口占用情况及处理方式

    1. 在Mac下端口占用的情况: 找到占用的进程并杀掉: 1.查看端口占用进程 sudo lsof -i :8880 可以看到进程的PID 2.杀掉进程 sudo kill -9 4580(4580为 ...

  3. 杂项-SpringBoot-Jasypt:Jasypt(安全框架)

    ylbtech-杂项-SpringBoot-Jasypt:Jasypt(安全框架) 1. 使用jasypt加密Spring Boot应用中的敏感配置返回顶部 1. 本文讲述了在Spring Boot/ ...

  4. 转:五种I/O模型和select函数简介

    源地址:http://blog.csdn.net/jnu_simba/article/details/9070955 一.五种I/O模型 1.阻塞I/O 我们在前面所说的I/O模型都是阻塞I/O,即调 ...

  5. Python学习day08-python进阶(2)-内置方法

    Python学习day08-python进阶(2)-内置方法 列表数据类型内置方法 作用 描述多个值,比如爱好 定义方法       xxxxxxxxxx 2         1 hobby_list ...

  6. [转]Expression Blend实例中文教程(8) - 动画设计快速入门StoryBoard

    上一篇,介绍了Silverlight动画设计基础知识,Silverlight动画是基于时间线的,对于动画的实现,其实也就是对对象属性的修改过程. 而Silverlight动画分类两种类型,From/T ...

  7. Vue+jquery上拉加载

    <ul> <li class="new-list" v-for="item in proarr"> <a :href=" ...

  8. mysql innodb 的 逻辑存储结构

    如上图: innodb 的 逻辑存储单元分成 表空间,段,区,页 4个等级 默认情况下,一个数据库 所有变共享一个 默认的表空间(tablespan).可以指定每个表一个表空间. 一个表空间管理着 多 ...

  9. PAT甲级——A1033 To Fill or Not to Fill

    With highways available, driving a car from Hangzhou to any other city is easy. But since the tank c ...

  10. oracle数据库忘记sys密码如何改密码

    ORACLE服务器操作: 1.win+R打开dos窗口cmd 2.输入 sqlplus/nolog出现 3.输入 conn / as sysdba 出现 4. alter user sys ident ...