题解-ARC058D Iroha Loves Strings
题面
给定 \(n\) 个字符串,从中选出若干个按给出顺序连接起来,总长等于 \(m\),求字典序最小的,保证有解。
数据范围:\(1\le n\le 2000\),\(1\le k\le 10^4\),字符串总长 \(S\le 10^6\)。
题解
atcoder 的题就是好啊,非常巧妙,毫不毒瘤。
这篇题解要抨击 Z-function 怪 ycx,造福人民。
有一个非常显然的思路:每次选当前选了的串右边没选且能选的中最小的。
这里的能选的可以用个后缀背包判断,可以用 bitset 维护。
注意到对于每个长度 \(i\) 能不能由后缀拼成有个分界点,设为 \(r_i\)。
这个做法有个明显的问题:如果有一个串 geo 和一个串 geoafo,怎么知道哪个好呢?
如果选了
geo,如果剩下的串只有boomzero,那么就不如geoafo了。如果选了
geoafo,如果剩下的串有个aba,那么就geoaba更优了。
所以想到用 dp,来避免不同长度的字符串的比较。
设 \(f_i\) 表示拼成的长度为 \(i\) 且剩下字符串可以拼成 \(m-i\) 的字符串中字典序最小的(注意 \(f_i\) 是个字符串!)。
然后 \(g_i\) 是拼成 \(f_i\) 的最右字符串的编号,要最左。
转移就是从小到大枚举每个 \(i\)(前提是有这样的 \(f_i\)),然后枚举加上的字符串长度 \(j\)。
要找到 \(g_i\) 到 \(r_{m-i-j}\) 中长度为 \(j\) 的字典序最小字符串,这个可以用 st 表预处理 \(\Theta(1)\) 完成。
然后把这个串加到 \(f_i\) 后面暴力和 \(f_{i+j}\) 比较即可。
关于时间复杂度,总感觉是 \(\Theta(n^2)\) 的,但是不会证,就当 \(\Theta(n^3)\) 的吧(可是它明明跑得很快 /kk)。
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
#define x first
#define y second
#define bg begin()
#define ed end()
#define pb push_back
#define mp make_pair
#define sz(a) int((a).size())
#define R(i,n) for(int i(0);i<(n);++i)
#define L(i,n) for(int i((n)-1);i>=0;--i)
const int iinf=0x3f3f3f3f;
const ll linf=0x3f3f3f3f3f3f3f3f;
//Data
const int N=2000+1,M=1e4+1,D=12;
int n,m,o[N],we[N],ri[M],g[M];
vector<int> id[M],st[M][D];
string s[N],f[M];
bitset<M> h[N];
//Functions
int get(int j,int l,int r){
l=lower_bound(id[j].bg,id[j].ed,l)-id[j].bg;
r=lower_bound(id[j].bg,id[j].ed,r)-id[j].bg;
if(r-l<=0) return -1;
int t=log2(r-l),&a=st[j][t][l],&b=st[j][t][r-(1<<t)];
return we[a]>we[b]?b:a;
}
//Main
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n>>m,h[n].set(0);
R(i,n) cin>>s[o[i]=i];
stable_sort(o,o+n,[&](int i,int j){return s[i]<s[j];});
R(i,n) we[o[i]]=i,id[sz(s[i])].pb(i);
R(i,m+1){
g[i]=-1,ri[i]=0;
vector<int> &c=id[i],*a=st[i];
if(!sz(c)) continue;
R(i,D) a[i].resize(sz(c));
R(i,sz(c)) a[0][i]=c[i];
R(t,D-1)R(i,sz(c)+1-(2<<t)){
int &x=a[t][i],&y=a[t][i+(1<<t)];
a[t+1][i]=we[x]>we[y]?y:x;
}
}
L(i,n){
h[i]=h[i+1]|h[i+1]<<sz(s[i]);
R(j,m+1)if(h[i][j]&&!h[i+1][j]) ri[j]=i;
}
f[0]="",g[0]=0,ri[0]=n;
R(j,m)if(~g[j])L(i,m+1-j){
int t=get(i,g[j],ri[m-j-i]);
// cout<<"j="<<j<<"->j+i="<<j+i<<'\n';
// cout<<"get("<<i<<","<<g[j]<<","<<ri[m-j-i]<<")="<<t<<'\n';
if(~t&&(!~g[j+i]||f[j+i]>f[j]+s[t]))
f[j+i]=f[j]+s[t],g[j+i]=t+1;
}
// R(i,m+1) cout<<g[i]<<" "<<f[i]<<'\n';
cout<<f[m]<<'\n';
return 0;
}
祝大家学习愉快!
题解-ARC058D Iroha Loves Strings的更多相关文章
- 文字列大好きいろはちゃんイージー / Iroha Loves Strings (ABC Edition) (优先队列)
题目链接:http://abc042.contest.atcoder.jp/tasks/abc042_b Time limit : 2sec / Memory limit : 256MB Score ...
- Atcoder Regular Contest 058 D - 文字列大好きいろはちゃん / Iroha Loves Strings(单调栈+Z 函数)
洛谷题面传送门 & Atcoder 题面传送门 神仙题. mol 一发现场(bushi)独立切掉此题的 ycx %%%%%%% 首先咱们可以想到一个非常 naive 的 DP,\(dp_{i, ...
- Codeforces Round #254 (Div. 1) D - DZY Loves Strings
D - DZY Loves Strings 思路:感觉这种把询问按大小分成两类解决的问题都很不好想.. https://codeforces.com/blog/entry/12959 题解说得很清楚啦 ...
- Codeforces Round #254 (Div. 1) D. DZY Loves Strings hash 暴力
D. DZY Loves Strings 题目连接: http://codeforces.com/contest/444/problem/D Description DZY loves strings ...
- [CodeForces - 447B] B - DZY Loves Strings
B - DZY Loves Strings DZY loves collecting special strings which only contain lowercase letters. For ...
- 【题解】DZY Loves Chinese
[题解]DZY Loves Chinese II 不吐槽这题面了... 考虑如何维护图的连通性,如果把图的变成一颗的\(dfs\)生成树,那么如果把一个节点的父边和他接下来所有的返祖边删除,那么我们就 ...
- Codeforces Round #FF (Div. 2):B. DZY Loves Strings
B. DZY Loves Strings time limit per test 1 second memory limit per test 256 megabytes input standard ...
- CF447B DZY Loves Strings 题解
Content 有一个长度为 \(n\) 的仅含小写字母的字符串 \(s\) 以及 26 个英文小写字母的价值 \(W_\texttt{a},W_\texttt{b},...,W_\texttt{z} ...
- CF447B DZY Loves Strings 贪心
DZY loves collecting special strings which only contain lowercase letters. For each lowercase letter ...
随机推荐
- 手写atoi、strcpy、strcat
一:实现atoi函数 1 #include<iostream> 2 3 using namespace std; 4 5 int atoi_my(const char *str) 6 { ...
- 01 Servlet技术
Servlet 1.Servlet技术 1.1.什么是Servlet Servlet是JavaEE规范之一.规范就是接口 Servlet就JavaWeb三大组件之一.三大组件分别是:Servlet程序 ...
- 菜鸟试做GUI简单数据库查询界面 python+tkinter+mysql
一.准备工作: 1.安装mysql3.7,创建一个test数据库,创建student表,创建列:(列名看代码),创建几条数据 (以上工作直接用navicat for mysql工具完成) 二.代码: ...
- 使用qmake编译时拷贝文件
使用qmake构建项目时,需要在make时拷贝一些文件到指定位置,非执行make install,下面总结列举一下我了解的方式 COPIES 示例pro 1 COPY_DIR = $$PWD/copy ...
- linux利用screen进行shell下的屏幕协作
我们都知道linux是支持多终端并行处理的 但是某些时候我们可能有比较特殊的需求需要两个人同时处理一个终端,screen 正好能满足这个要求 首先需要安装screen软件: debian和ubuntu ...
- Python _PyQt5对话框
Python 调用PyQt5 制作对话框,退出时候有二次确认(注:默认是直接退出) 1 # -*- ytf-8 -*- 2 """ 3 用PyQt建一个对话框,退出时提示 ...
- oracle 11.2.0.4静默安装
oracle 11.2.0.4静默安装 1.安装包 1.1.上传安装包 xshell可用rz命令,选择安装包. mobaxterm可用左侧栏上传功能. 2.安装准备 2.1.关闭防火墙.SELinux ...
- Qt For MacOs环境搭建
使用VMWARE关于macos镜像搭建,参考https://blog.csdn.net/u011415782/article/details/78505422 关于darwin8.5.5 来安装vmt ...
- 使用XSL解析XML输出HTML(XSL学习笔记一)
最近项目用到 XSL + XML,XML大家应该很熟悉,XSL暂且不解释,先看效果,如果想学习XSL的内容,可以先访问: https://www.w3school.com.cn/xsl/xsl_lan ...
- PHP的命令执行漏洞学习
首先我们来了解基础 基础知识来源于:<web安全攻防>徐焱 命令执行漏洞 应用程序有时需要调用一些执行系统命令的函数,如在PHP中,使用system.exec.shell_exec.pas ...