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. Doris 再次启动FE失败的思考

    Doris再次启动FE失败的思考 背景描述 在昨天已经成功下载安装最新稳定版docker.拉取doris-0.15.0版本的镜像.将镜像挂载道本地Doris源码目录.完成了doris的编译之后,今天在 ...

  2. 《最新出炉》系列初窥篇-Python+Playwright自动化测试-14-playwright操作iframe-番外篇

    1.简介 通过前边三篇的学习,想必大家已经对iframe有了一定的认识和了解,今天这一篇主要是对iframe的一些特殊情况的介绍和讲解,主要从iframe的定位.监听事件和执行js脚本三个方面进行展开 ...

  3. .NET C#基础(9):资源释放 - 需要介入的资源管理

    1. 什么是IDisposable?   IDisposable接口是一个用于约定可进行释放资源操作的接口,一个类实现该接口则意味着可以使用接口约定的方法Dispose来释放资源.其定义如下: pub ...

  4. redisson分布式锁的应用——秒杀、超卖 简单例子(分布式锁相关)

    1.常见的分布式事务锁 1.数据库级别的锁 乐观锁,给予加入版本号实现 悲观锁,基于数据库的for update实现 2.Redis,基于SETNX.EXPIRE实现 3.Zookeeper,基于In ...

  5. Z-Blog火车头免登录发布教程+插件3.2+支持最新Z-Blog1.7

    Z-Blog免登录采集评论,之前没有加入评论接口,今天把评论接口写好了,写一下简单的教程,(采集评论规则是一件很麻烦的事)有时候采集文章的时候也采集评论,今天教大家怎样用我的Z-Blog免登录采集插件 ...

  6. Docker Swarm + Harbor + Portainer 打造高可用,高伸缩,集群自动化部署,更新。

    Docker Swarm是Docker官方自带的容器编排工具,Swarm,Compose,Machine合称Docker三剑客.Docker Swarm对于中小型应用来说,还是比较方便,灵活,当然K8 ...

  7. 使用go语言开发hive导出工具

    前言 新版 hive 提供了 beeline 工具,可以执行SQL并导出数据,不过操作还是有点复杂的,团队里有些同学不会Linux的基本操作,所以我花了亿点点时间写了个交互式的命令行工具方便使用. 效 ...

  8. 数据链路层传输协议(点到点):停等协议、GBN、SR协议

    数据链路层的传输协议:停等协议.GBN.SR 停止等待协议(单窗口的滑动窗口协议) 滑动窗口协议:GBN.SR GBN协议 GBN发送方需响应的三件事 1. 上层调用(网络层) 上层要发送数据时,发送 ...

  9. 17.1 隐藏执行CMD命令

    本章内容涉及使用Socket API和CMD命令行工具实现本地CMD命令执行.无管道正向CMD和无管道反向CMD三种功能.执行本地CMD实现使用CreateProcess函数创建一个新的CMD进程,并 ...

  10. ES6和node模块化

    node模块化: 1.输出:exports.a=12; 或者module.exports={ a:12, b:5 } 2.引入:require('./a.js'); 3.引用自定义模块 放到node_ ...