计蒜客 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 问题:对于每个询问的起点终点,求 ...
随机推荐
- jquery 判断当前设备是PC端还是移动端
$(function(){ var system = { win: false, mac: false, xll: false, ipad:false }; //检测平台 var p = naviga ...
- 纯 CSS 实现实心三角形和空心三角形
一.实心三角形 1.代码 .div-angles{ width:; height:; border-style: solid; border-width:30px; border-color: tra ...
- shell脚本实现读取一个文件中的某一列,并进行循环处理
shell脚本实现读取一个文件中的某一列,并进行循环处理 1) for循环 #!bin/bash if [ ! -f "userlist.txt" ]; then echo &qu ...
- vue报错 error: data.push is not a function
data定义出错,data需定义为一个数组 => data : [ ]
- java linkedlist和arraylist添加元素时性能比较
- vue.js_08_vue-组件的定义
1.vue组件常用定义方式 <body> <div id="app"> <!--1.3使用组件--> <mycom1></my ...
- MySQL命令行本地登陆,远程登陆MySQL 的快捷键
1.进入Mysql的安装目录bin文件夹下 如默认路径: cd C:\Program Files\MySQL\MySQL Server 8.0\bin 2.本地登录MySQL 命令:mysql -u ...
- java监控文件夹下的文件变化使用jnotify
https://blog.csdn.net/codepython/article/details/42341243?utm_source=blogxgwz1 使用jnotify https://blo ...
- PhoneInfoga---用于电话号码的信息收集和OSINT侦察工具
PhoneInfoga 是仅使用免费资源扫描电话号码的最先进工具之一. 目标是首先在任何国际电话号码上收集标准信息, 如国家,地区,运营商和线路类型,并且准确性非常高. 然后在搜索引擎上搜索足迹以尝试 ...
- 【DM8168学习笔记2】DM8168 EZSDK 结构
1 2 3 4 5