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. java基础之Random类

    Random类 Random类中实现的随机算法是伪随机,也就是有规则的随机.在进行随机时,随机算法的起源数字称为种子数(seed), 在种子数的基础上进行一定的变换,从而产生需要的随机数字. 相同种子 ...

  2. 提前关闭Scrapy爬虫的设置

    Scrapy的CloseSpider扩展会在满足条件时自动终止爬虫程序.可以设置CLOSESPIDER_TIMEOUT(秒).CLOSESPIDER_ITEMCOUNT.CLOSESPIDER_PAG ...

  3. sqlmap:入门(手工注入)

    一. 联合查询注入union(less-1) 1. union操作符用于合并两个或多个select语句结果集: 2. union后的select语句必须拥有和最前的select语句拥有相同数量的字段, ...

  4. QC的安装和配置

    QC(Quality center)的安装配置 Wmware 虚拟机 数据库SQL server2000 Windows server 2003 须安装数据库的sp4补丁包 注意事项 数据库安装时选择 ...

  5. 从0开始学习ssh之资源分类

    更目录下面,新建config用于放配置文件,新建test用于放置测试文件.src目录用于放置源代码.由于ssh是三层,因此新建三层包(dao,service,view).其中dao和service还有 ...

  6. 嘴巴题5 「BZOJ1864」[ZJOI2006] 三色二叉树

    1864: [Zjoi2006]三色二叉树 Time Limit: 1 Sec Memory Limit: 64 MB Submit: 1195 Solved: 882 [Submit][Status ...

  7. 如何使用log4j记录日志

    1.下载jar包 http://logging.apache.org/log4j 2.将jar包加入项目 放在lib(没有就创建)下 对已经复制过来的jar包鼠标点击右键,选中BuildPath  - ...

  8. 深入浅出 Java Concurrency (11): 锁机制 part 6 CyclicBarrier[转]

    如果说CountDownLatch是一次性的,那么CyclicBarrier正好可以循环使用.它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point).所谓屏障点就 ...

  9. [C#] double指定有效位数格式化

    C#里面指定小数位数格式化大家都知道 ff.ToString("F3") 可以指定精确到三位小数. 但是如何指定有效位数呢?方法是 ff.ToString("G3&quo ...

  10. jeecms v9.3 has a stroed xss vulnerability

    转载:https://blog.csdn.net/libieme/article/details/83588929 jeecms v9.3 has a stroed xss vulnerability ...