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 abcdefghijklmnopqrstuvwxyz of 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.

  1. $|S| \lt |T|$ and $S_1S_2\ldots S_{|S|} = T_1T_2\ldots T_{|S|}$.
  2. 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的更多相关文章

  1. mysql删除重复记录,保存Id最小的一条

    方法1:1.创建一个临时表,选取需要的数据.2.清空原表.3.临时表数据导入到原表.4.删除临时表.mysql> select * from student;+----+------+| ID ...

  2. id生成策略 id工具类

    import java.util.Random; /** * 各种id生成策略 * <p>Title: IDUtils</p> * <p>Description: ...

  3. 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 ...

  4. oracle创建表(并且实现ID自增)

    CREATE TABLE STUDENT ( ID INT NOT NULL, NAME VARCHAR2(4000) NOT NULL, PRIMARY KEY(ID) ) TABLESPACE M ...

  5. (转载)在mysql中,column 'id' in field list is ambiguous

    (转载)http://blog.chinaunix.net/uid-20665047-id-3137284.html column 'id' in field list is ambiguous 这个 ...

  6. OC的特有语法-分类Category、 类的本质、description方法、SEL、NSLog输出增强、点语法、变量作用域、@property @synthesize关键字、Id、OC语言构造方法

    一. 分类-Category 1. 基本用途:Category  分类是OC特有的语言,依赖于类. ➢ 如何在不改变原来类模型的前提下,给类扩充一些方法?有2种方式 ● 继承 ● 分类(Categor ...

  7. 针对Student表的DAO设计实例

    完整代码以及junit,mysql--connector包下载地址 : https://github.com/CasterWx/MyStudentDao 表信息: 代码: dao包----impl包- ...

  8. Spring RPC 入门学习(3)-插入Student对象

    Spring RPC 向后台传递对象 1. 新建RPC接口:StudentInterface.java package com.cvicse.ump.rpc.interfaceDefine; impo ...

  9. id生成工具类

    import java.util.Random; /** * 各种id生成策略 * <p>Title: IDUtils</p> * <p>Description: ...

  10. oracle中实现自增id

    在一些数据库(例如mysql)中,实现自增id只要在建表的时候指定一下即可, 但是在oracle中要借助sequence来实现自增id, 要用上自增id,有几种方式: 1.直接在insert语句中使用 ...

随机推荐

  1. 如何使用API接口获取Lazada商品详情数据

    随着电商市场的不断发展壮大,越来越多的人开始选择在网上购买商品.其中,东南亚地区的Lazada电商平台备受欢迎.如果您是一名电商从业者,或者打算在Lazada上开店,那么获取商品详情信息将是一个非常重 ...

  2. pygame 入门实例教程 1 - 复古方块赛车游戏

    作者自我介绍:大爽歌, b站小UP主 ,直播编程+红警三 ,python1对1辅导老师 . 本教程步骤明确,过程清晰简明,最终代码量250行上下,适合学习pygame的新手. 项目代码已上传到我的gi ...

  3. [面向对象] 魔术方法 (__set, __get, __unset, __isset)

    __set, __get,__isset, __unset 是面向对象里用来友操作的魔术方法.  先看看使用方法 echo $类->属性;  //取不存在属性或私有保护属性时,  以下方法被调用 ...

  4. 如何实现一个数据库的 UDF?图数据库 NebulaGraph UDF 功能背后的设计与思考

    大家好,我是来自 BOSS直聘的赵俊南,主要负责安全方面的图存储相关工作.作为一个从 v1.x 用到 v3.x 版本的忠实用户,在见证 NebulaGraph 发展的同时,也和它一起成长. BOSS直 ...

  5. 云原生(docker jenkins k8s)docker篇

    docker (1)架构 ● Docker_Host: ○ 安装Docker的主机 ● Docker Daemon: ○ 运行在Docker主机上的Docker后台进程 ● Client: ○ 操作D ...

  6. Spring框架中的设计模式(重点学习!!!)

    Spring中的设计模式 Spring框架中用到的设计模式有很多,以下是一些常见的设计模式: 依赖注入(DI)和控制反转(IoC):这是Spring框架最核心的设计模式,它允许开发人员将对象之间的依赖 ...

  7. 痞子衡嵌入式:MCUBootUtility v5.3发布,利用XMCD轻松使能外部RAM

    -- 痞子衡维护的 NXP-MCUBootUtility 工具距离上一个大版本(v5.0.0)发布过去4个多月了,期间痞子衡也做过三个小版本更新,但不足以单独介绍.这一次痞子衡为大家带来了全新重要版本 ...

  8. DBeaver Ultimate 22.1.0 连接数据库(MySQL+Mongo+Clickhouse)

    前言 继续书接上文 Docker Compose V2 安装常用数据库MySQL+Mongo,部署安装好之后我本来是找了一个web端的在线连接数据库的工具,但是使用过程中并不丝滑,最终还是选择了使用 ...

  9. IEEE 国际计算科学与工程会议 (CSE-2023)

    随着计算机系统变得越来越庞大和复杂,基于数据的计算技术在支持下一代科学和工程应用方面发挥着关键作用.如今,科学和工程中基于云的复杂大数据应用由异构软件/硬件/网络组件组成,这些组件的容量.可用性和环境 ...

  10. 触发器引起的ADG备库同步错误

    数据库alert日志报错ORA-16000,查看对应的trc文件,大致如下报错: *** 2020-10-27 14:09:03.340*** SESSION ID:(3340.75) 2020-10 ...