计蒜客 Prefix Free Code(字典树+树状数组)
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(字典树+树状数组)的更多相关文章
- 计蒜客 青出于蓝胜于蓝(dfs序+树状数组)
题目描述 武当派一共有 n 人,门派内 n 人按照武功高低进行排名,武功最高的人排名第 1,次高的人排名第 2,... 武功最低的人排名 第 n.现在我们用武功的排名来给每个人标号,除了祖师爷,每个人 ...
- 计蒜客 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 ...
- 计蒜客 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 ...
- 计蒜客16492 building(二分线段树/分块)
题解: 考虑用线段树维护楼的最大值,然后这个问题就很简单了. 每次可以向左二分出比x高的第一个楼a,同理也可以向右二分出另一个楼b,如果a,b都存在,答案就是b-a-1. 注意到二分是可以直接在线段树 ...
- 计蒜客 28449.算个欧拉函数给大家助助兴-大数的因子个数 (HDU5649.DZY Loves Sorting) ( ACM训练联盟周赛 G)
ACM训练联盟周赛 这一场有几个数据结构的题,但是自己太菜,不会树套树,带插入的区间第K小-替罪羊套函数式线段树, 先立个flag,BZOJ3065: 带插入区间K小值 计蒜客 Zeratul与Xor ...
- 爬虫acm比赛成绩(多页成绩整合在一起、获取复制不了的数据)(hihocoder、计蒜客)
https://github.com/congmingyige/web-crawler_rank-of-competition-in-JiSuanKe-and-hihocoder 1. 计蒜客(获取复 ...
- 计蒜客 NOIP 提高组模拟竞赛第一试 补记
计蒜客 NOIP 提高组模拟竞赛第一试 补记 A. 广场车神 题目大意: 一个\(n\times m(n,m\le2000)\)的网格,初始时位于左下角的\((1,1)\)处,终点在右上角的\((n, ...
- 计蒜客 A1607 UVALive 8512 [ACM-ICPC 2017 Asia Xi'an]XOR
ICPC官网题面假的,要下载PDF,点了提交还找不到结果在哪看(我没找到),用VJ交还直接return 0;也能AC 计蒜客题面 这个好 Time limit 3000 ms OS Linux 题目来 ...
- [计蒜客] 矿石采集【记搜、Tarjan缩点+期望Dp】
Online Judge:计蒜客信息学3月提高组模拟赛 Label:记搜,TarJan缩点,树状数组,期望Dp 题解 整个题目由毫无关联的两个问题组合成: part1 问题:对于每个询问的起点终点,求 ...
随机推荐
- css实现图文混排
css实现图文混排 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...
- C#可扩展编程之MEF(四):见证奇迹的时刻
前面三篇讲了MEF的基础和基本到导入导出方法,下面就是见证MEF真正魅力所在的时刻.如果没有看过前面的文章,请到我的博客首页查看. 前面我们都是在一个项目中写了一个类来测试的,但实际开发中,我们往 ...
- 【源码】PyObject_VAR_HEAD 定长对象 变长对象
PyObject_VAR_HEAD Python-3.7.4\Include\object.h /* PyObject_VAR_HEAD defines the initial segm ...
- 郝斌–SQL Server2005学习笔记
数据库(Database)狭义上是指存储数据的仓库,广义上包含对数据进行存储和管理的软件(DBMS)和数据本身.数据库由表.关系和操作组成. 一.数据库简介 1.为什么需要数据库 数据库简化了对数据的 ...
- Linux 日期时间命令
cal : 显示日历 -1 显示一个月的月历 -3 显示系统前一个月,当前月,下一个月的月历 -s 显示星期天为一个星期的第一天,默认的格式 -m 显示星期一为一个星期的第一天 -j 显示在当年中 ...
- 关于MQ 消息队列的通俗理解和 rabbitMQ 使用
消息队列,一听很高大上,现在很多分布式系统都在用这个消息中间件 网上一搜, 说的都是些原理. 说下我的通俗理解, 你网上买了, 快递员给你投递, 会出现什么问题呢? 1 你不定时在家, 快递员 来了 ...
- Activiti数据库
数据库 Activiti的后台是有数据库的支持,所有的表都以ACT_开头. 第二部分是表示表的用途的两个字母标识. 用途也和服务的API对应. 1) ACT_RE_*: 'RE'表示repos ...
- np一些基本操作1
##生成一个一维数组import numpy as np;nb7 = np.arange(0,100,2);print(nb7)print("======================== ...
- centos Python2.6 升级到2.7
需求: centos 6.x 系统默认Python 版本都是2.6,实际生产环境中需要用到Python 2.7.x Python 2.7 下载地址 [root@ansible package]# wg ...
- putty开源的ssh软件工具
# 登录远程服务器需要ip和端口即可:还是开源工具用起来无忧无虑.无拘无束,这种感觉实在太舒服了,比起xshell开始免费.后来收费好太多太多,不用担心哪天过期了,想干嘛就干嘛. 软件下载地址:htt ...