汕头市队赛 SRM19 字符题
2024-08-24 12:29:11
从天上掉下来了个这样的问题:
有一个字符串 从中选出两个子串 A,B,求 A+B可以构成的不同串的个数。 还想知道,这么多个串中字典序最大的那一个。
某人捡到了这个问题,并把它扔给了你。
【输入】
一个全由小写字母构成的字符串。
【输出】
第一行 一个非负整数,表示两个子串A+B可以构成的不同串个数。由于答案可能很大,所以答案对1004535809取模。
第二行 一个字符串,表示构成的串中字典序最大的。
【样例输入1】
ab
【样例输出1】
11
bb
【样例输入2】
abcaabccba
【样例输出2】
1428
ccccba
【数据范围及约定】
n=|S|
10% n<=10
30% n<=40
另有20% 字符串由1个a和n-1个b构成
100% n<=2000
【提示】
两个子串均可为空,但不同时为空。
这道题我们可以建一棵trie树记录所有子串 计算呢为了防止算重复
我们可以枚举一个子串末尾加上某个字母后是否还是子串
然后就搞来搞去就可以了 至于最大的话就跑一下字典序最大的子串
前面再扔整个序列里面最大的那个字母 能扔几个是几个
#include<cstdio>
#include<cstring>
#include<algorithm>
const int M=5e3+,mod=;
int read(){
int ans=,f=,c=getchar();
while(c<''||c>''){if(c=='-') f=-; c=getchar();}
while(c>=''&&c<=''){ans=ans*+(c-''); c=getchar();}
return ans*f;
}
char ch[M],ansq[M];
int ans,s[M],cnt,n,cntq;
int c[][];
void insert(int k){
int rt=;
for(int i=k;i<=n;i++){
if(!c[rt][s[i]]) c[rt][s[i]]=++cnt;
rt=c[rt][s[i]];
}
}
int A[],B[];
int dfs(int p){
int sz=;
for(int i=;i<;i++){
if(!c[p][i]) A[i]+=(p!=);
else if(!p) B[i]=dfs(c[p][i]);
else sz+=dfs(c[p][i]);
}
return sz;
}
int main(){
scanf("%s",ch+);
n=strlen(ch+);
for(int i=;i<=n;i++) s[i]=ch[i]-'a';
for(int i=;i<=n;i++) insert(i);
dfs();
for(int i=;i<;i++) ans=(ans+1LL*(A[i]+)*B[i]%mod)%mod;
printf("%d\n",ans);
int rt=;
while(){
for(int i=;i>=;i--)if(c[rt][i]){
ansq[++cntq]=i+'a';
rt=c[rt][i]; goto o;
}
break;
o:;
}
for(int i=;i<=cntq;i++)
if(ansq[i]==ansq[]) putchar(ansq[i]);
else break;
printf("%s",(ansq+));
return ;
}
最新文章
- 读懂IL代码就这么简单(二)
- Unreal Engine4 学习笔记2 动画蒙太奇
- 【MySQL】InnoDB: Error: checksum mismatch in data file 报错
- PHP_ArrayList
- Hubot Slack CoffeeScript
- 远程测试mysql数据库3306端口报错
- IO流的应用————小型资源管理器
- R与数据分析旧笔记(十四) 动态聚类:K-means
- DBA 应该要注意Linux 环境下的一些操作
- Swift - 类扩展(extension)
- 第十节,While循环和for循环
- 基于Win32 SDK实现的一个简易线程池
- python csv例子
- JVM-4.类加载机制
- 使用tdload工具将本地数据导入到Teradata数据库中
- 题解——loj6281 数列分块入门5 (分块)
- SSH 证书登录(实例详解)
- [uboot]uboot中am437对应的GPIO配置
- September 20th 2017 Week 38th Wednesday
- ubuntu16.04常见的问题解决方案
热门文章
- 福大软工1816:Alpha(7/10)
- C语言特殊符号
- 如何设置windows 2003的最大远程连接数
- chrome扩展程序中以编程方式插入内容脚本不生效的问题
- BZOJ 1406 密码箱(数论)
- [CF622F]The Sum of the k-th Powers
- python安装方法- 3.6.3版本
- BZOJ4698 &; 洛谷2463:[SDOI2008]Sandy的卡片——题解
- oracle行转列函数WMSYS.WM_CONCAT 用法
- Linux系统上的popen()库函数