[ABC268G] Random Student ID
Problem Statement
Takahashi Elementary School has $N$ new students. For $i = 1, 2, \ldots, N$, the name of the $i$-th new student is $S_i$ (which is a string consisting of lowercase English letters).
The names of the $N$ new students are distinct.
The $N$ students will be assigned a student ID $1, 2, 3, \ldots, N$ in ascending lexicographical order of their names. However, instead of the ordinary order of lowercase English letters where a is the minimum and z is the maximum, we use the following order:
- First, Principal Takahashi chooses a string $P$ from the $26!$ permutations of the string
abcdefghijklmnopqrstuvwxyzof length $26$, uniformly at random. - The lowercase English characters that occur earlier in $P$ are considered smaller.
For each of the $N$ students, find the expected value, modulo $998244353$, of the student ID assigned (see Notes).
What is the lexicographical order?
A string $S = S_1S_2\ldots S_{|S|}$ is said to be lexicographically smaller than a string $T = T_1T_2\ldots T_{|T|}$ if one of the following 1. and 2. holds.
Here, $|S|$ and $|T|$ denote the lengths of $S$ and $T$, respectively.
- $|S| \lt |T|$ and $S_1S_2\ldots S_{|S|} = T_1T_2\ldots T_{|S|}$.
- There exists an integer $1 \leq i \leq \min\lbrace |S|, |T| \rbrace$ satisfying the following two conditions:
- $S_1S_2\ldots S_{i-1} = T_1T_2\ldots T_{i-1}$
- $S_i$ is a smaller character than $T_i$.
Notes
We can prove that the sought expected value is always a rational number. Moreover, under the Constraints of this problem, when the value is represented as $\frac{P}{Q}$ by two coprime integers $P$ and $Q$, we can prove that there is a unique integer $R$ such that $R \times Q \equiv P\pmod{998244353}$ and $0 \leq R \lt 998244353$. Find such $R$.
Constraints
- $2 \leq N$
- $N$ is an integer.
- $S_i$ is a string of length at least $1$ consisting of lowercase English letters.
- The sum of lengths of the given strings is at most $5 \times 10^5$.
- $i \neq j \Rightarrow S_i \neq S_j$
Input
Input is given from Standard Input in the following format:
$N$
$S_1$
$S_2$
$\vdots$
$S_N$
Output
Print $N$ lines.
For each $i = 1, 2, \ldots, N$, the $i$-th line should contain the expected value, modulo $998244353$, of the student ID assigned to Student $i$.
Sample Input 1
3
a
aa
ab
Sample Output 1
1
499122179
499122179
The expected value of the student ID assigned to Student $1$ is $1$; the expected values of the student ID assigned to Student $2$ and $3$ are $\frac{5}{2}$.
Note that the answer should be printed modulo $998244353$.
For example, the sought expected value for Student $2$ and $3$ is $\frac{5}{2}$,
and we have $2 \times 499122179 \equiv 5\pmod{998244353}$,
so $499122179$ should be printed.
Sample Input 2
3
a
aa
aaa
Sample Output 2
1
2
3
期望有线性法则。
和的期望等于期望的和。这里要求排名,而每次有一个比他小ID,对排名的贡献是1.最后要求贡献之和的期望,也可以拆成若干个期望之和。
考虑在trie树上弄,那么一个单词的末尾节点 \(x\),如果他的祖先也为单词节点,那么这个节点的字典序一定比他小,算入最终排名。如果以这个节点为根的子树有单词节点,那么这些单词字典序一定比他大。其他的单词超过这个单词的概率为 \(\frac 12\),对排名贡献的期望也是 \(\frac 12\),统计即可。
#include<bits/stdc++.h>
const int N=5e5+5,P=998244353,inv2=P+1>>1;
int n,tr[N][26],idx,sz[N],tag[N],ans[N],cnt=1;
char s[N];
void insert(char s[],int i)
{
int len=strlen(s+1),u=0;
for(int i=1;i<=len;i++)
{
if(!tr[u][s[i]-'a'])
tr[u][s[i]-'a']=++idx;
u=tr[u][s[i]-'a'];
sz[u]++;
}
tag[u]=i;
}
void dfs(int x)
{
if(tag[x])
{
(ans[tag[x]]=1LL*inv2*(n-sz[x]-cnt+1)%P+cnt)%=P;
++cnt;
}
for(int i=0;i<26;i++)
if(tr[x][i])
dfs(tr[x][i]);
if(tag[x])
--cnt;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",s+1);
insert(s,i);
}
dfs(0);
for(int i=1;i<=n;i++)
printf("%d\n",ans[i]);
}
[ABC268G] Random Student ID的更多相关文章
- mysql删除重复记录,保存Id最小的一条
方法1:1.创建一个临时表,选取需要的数据.2.清空原表.3.临时表数据导入到原表.4.删除临时表.mysql> select * from student;+----+------+| ID ...
- id生成策略 id工具类
import java.util.Random; /** * 各种id生成策略 * <p>Title: IDUtils</p> * <p>Description: ...
- IPv6 tutorial – Part 7: Zone ID and unique local IPv6 unicast addresses
The zone ID is used to distinguish ambiguous link-local and site-local addresses. Unique local IPv6 ...
- oracle创建表(并且实现ID自增)
CREATE TABLE STUDENT ( ID INT NOT NULL, NAME VARCHAR2(4000) NOT NULL, PRIMARY KEY(ID) ) TABLESPACE M ...
- (转载)在mysql中,column 'id' in field list is ambiguous
(转载)http://blog.chinaunix.net/uid-20665047-id-3137284.html column 'id' in field list is ambiguous 这个 ...
- OC的特有语法-分类Category、 类的本质、description方法、SEL、NSLog输出增强、点语法、变量作用域、@property @synthesize关键字、Id、OC语言构造方法
一. 分类-Category 1. 基本用途:Category 分类是OC特有的语言,依赖于类. ➢ 如何在不改变原来类模型的前提下,给类扩充一些方法?有2种方式 ● 继承 ● 分类(Categor ...
- 针对Student表的DAO设计实例
完整代码以及junit,mysql--connector包下载地址 : https://github.com/CasterWx/MyStudentDao 表信息: 代码: dao包----impl包- ...
- Spring RPC 入门学习(3)-插入Student对象
Spring RPC 向后台传递对象 1. 新建RPC接口:StudentInterface.java package com.cvicse.ump.rpc.interfaceDefine; impo ...
- id生成工具类
import java.util.Random; /** * 各种id生成策略 * <p>Title: IDUtils</p> * <p>Description: ...
- oracle中实现自增id
在一些数据库(例如mysql)中,实现自增id只要在建表的时候指定一下即可, 但是在oracle中要借助sequence来实现自增id, 要用上自增id,有几种方式: 1.直接在insert语句中使用 ...
随机推荐
- Pandas 使用教程 Series、DataFrame
目录 Series (一维数据) 指定索引值 使用 key/value 对象,创建对象 设置 Series 名称参数 DataFrame(二维数据) 使用字典(key/value)创建 loc 属性返 ...
- uni-app+h5puls 编写相机拍照
<template> <view class="camera-page"> <image :src="imgSrc" v-if=& ...
- Linux 内核设备驱动程序的IO寄存器访问 (下)
Linux 内核设备驱动程序通过 devm_regmap_init_mmio() 等函数获得 struct regmap 结构对象,该对象包含可用于访问设备寄存器的全部信息,包括定义访问操作如何执行的 ...
- QA|20221002|SecureCRT中退格键变成了^H
原因:backspace键和delete键的键码映射问题 解决办法一:要使用回删键(backspace)时,同时按住ctrl键 解决办法二:重新设置码值映射关系.比如SecureCRT中,会话 ...
- 【算法】湖心岛上的数学梦--用c#实现一元多次方程的展开式
每天清晨,当第一缕阳光洒在湖面上,一个身影便会出现在湖心小岛上.她坐在一块大石头上,周围被茂盛的植物环绕,安静地沉浸在数学的世界中. 这个姑娘叫小悦,她的故事在这个美丽的湖心小岛上展开.每天早晨,她都 ...
- 文心一言 VS 讯飞星火 VS chatgpt (92)-- 算法导论8.3 5题
五.用go语言,在本节给出的第一个卡片排序算法中,为排序 d 位十进制数,在最坏情况下需要多少轮排序?在最坏情况下,操作员需要记录多少堆卡片? 文心一言: 你提到的第一个卡片排序算法可能是指的基数排序 ...
- 兴达易控modbus转profinet网关三菱变频器通讯
兴达易控modbus转profinet网关与三菱变频器通讯 本案例分享兴达易控modbus转profinet网关(MDPN100)连接西门子1200plc,实现三菱变频器485通讯兼容转modbusT ...
- NebulaGraph实战:3-信息抽取构建知识图谱
自动信息抽取发展了几十年,虽然模型很多,但是泛化能力很难用满意来形容,直到LLM的诞生.虽然最终信息抽取质量部分还是需要专家审核,但是已经极大的提高了信息抽取的效率.因为传统方法需要大量时间来完成 ...
- 如何选择适合你的HTAP数据库?
最近,在数据库行业对HTAP(混合事务/分析处理,Hybrid Transactional/Analytical Processing)这个概念宣传的非常火爆,也衍生出 Real-Time HTAP的 ...
- http 500 错误
214 [Thu Sep 09 16:03:54.919334 2021] [:error] [pid 32045] [client 124.238.184.230:23796] script '/v ...