题目链接:点击查看

题目大意:给出一个长度为 nnn 的字符串 sss,将其子串按顺序展开成序列,即 {s1,s1s2,⋯,s1s2…sn,s2,s2s3,s2s3…sn,s3,s3s4,⋯,sn−1sn,sn}\{s_1,s_1s_2,\cdots ,s_1 s_2 \ldots s_n,s_2,s_2 s_3, s_2 s_3\ldots s_n,s_3,s_3 s_4,\cdots,s_{n-1}s_n,s_n\}{s1​,s1​s2​,⋯,s1​s2​…sn​,s2​,s2​s3​,s2​s3​…sn​,s3​,s3​s4​,⋯,sn−1​sn​,sn​}

现在需要找到一个字典序递增的子序列,满足长度最长

题目分析:有个结论,如果子串 s[l:r]s[l:r]s[l:r] 被选择了的话,那么 s[l:r+1],s[l:r+2],⋯,s[l:n]s[l:r+1],s[l:r+2],\cdots,s[l:n]s[l:r+1],s[l:r+2],⋯,s[l:n] 也是会被选择的

不难看出 s[l:n]s[l:n]s[l:n] 是原串的一个后缀,所以不妨设 dp[i]dp[i]dp[i] 是以 s[i:n]s[i:n]s[i:n] 这个后缀结尾的最大答案

那么转移的话只需要枚举另一个后缀 s[j:n]s[j:n]s[j:n] 满足 j<ij<ij<i 就可以转移了,因为本题规定了字典序需要严格递增,快速比较两个后缀的字典序大小可以预处理一下 lcplcplcp ,而转移时,后缀 s[i:n]s[i:n]s[i:n] 的 lcplcplcp 的部分显然不满足字典序严格递增,需要剔除掉

代码:

// Problem: E. Rescue Niwen!
// Contest: Codeforces - Codeforces Round #741 (Div. 2)
// URL: https://codeforces.com/contest/1562/problem/E
// Memory Limit: 512 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<list>
#include<unordered_map>
#define lowbit(x) (x&-x)
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
template<typename T>
inline void read(T &x)
{T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f;
}
template<typename T>
inline void write(T x)
{if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0');
}
const int inf=0x3f3f3f3f;
const int N=5e3+100;
int lcp[N][N],dp[N];
char s[N];
int main()
{#ifndef ONLINE_JUDGE
//  freopen("data.in.txt","r",stdin);
//  freopen("data.out.txt","w",stdout);
#endif
//  ios::sync_with_stdio(false);int w;cin>>w;while(w--) {int n;read(n);scanf("%s",s+1);for(int i=1;i<=n+1;i++) {for(int j=1;j<=n+1;j++) {lcp[i][j]=0;}}for(int i=n;i>=1;i--) {for(int j=n;j>=1;j--) {if(s[i]==s[j]) {lcp[i][j]=lcp[i+1][j+1]+1;} else {lcp[i][j]=0;}}}for(int i=1;i<=n;i++) {dp[i]=n-i+1;for(int j=1;j<i;j++) {if(s[lcp[i][j]+i]>s[lcp[i][j]+j]) {dp[i]=max(dp[i],dp[j]+n-i+1-lcp[i][j]);}}}cout<<*max_element(dp+1,dp+1+n)<<endl;}return 0;
}

CodeForces - 1562E Rescue Niwen!(dp)相关推荐

  1. Codeforces Round #741 (Div. 2) E. Rescue Niwen! 字符串 + dp

    传送门 文章目录 题意: 思路: 题意: 给你一个串sss,定义其扩张串为s1,s1s2,...,s1s2..sn,s2,s2s3,...,sns_1,s_1s_2,...,s_1s_2..s_n,s ...

  2. cf1562E. Rescue Niwen!

    cf1562E. Rescue Niwen! 题意: 我们定义一个字符串s1s2s3-sn的展开为:s1,s1s2,s1s2-sn,s2,s2s3,s2s3-sn,-,sn 找到字符串s"扩 ...

  3. Codeforces 919D Substring (拓扑图DP)

    Codeforces 919D Substring (拓扑图DP) 手动博客搬家: 本文发表于20180716 10:53:12, 原地址https://blog.csdn.net/suncongbo ...

  4. [CodeForces 332B]Maximum Absurdity[DP]

    题目链接: [CodeForces 332B]Maximum Absurdity[DP] 题意分析: 寻找两个不重叠的长度为k的子串,使得它们之和最大. 解题思路: 第一想法是,处理出从这个点开始,长 ...

  5. 【CodeForces 1042B --- Vitamins】DP+位运算

    [CodeForces 1042B --- Vitamins]DP+位运算 题目来源:点击进入[CodeForces 1042B - Vitamins] Description Berland sho ...

  6. [CodeForces 300D Painting Square]DP

    http://codeforces.com/problemset/problem/300/D 题意:每一次操作可以选一个正方形,令边长为n,如果n为奇数那么可以从中间画一个十字,分成4个大小相等的边长 ...

  7. Codeforces 1322D Reality Show (DP)

    题目链接 https://codeforces.com/contest/1322/problem/D 题面写得非常模糊,很容易读错题,建议参考翻译:https://www.luogu.com.cn/p ...

  8. CodeForces - 1579G Minimal Coverage(dp)

    题目链接:点击查看 题目大意:给出 nnn 个长度不同的木棍.设第 i−1i-1i−1 次放置木棍后的终点为 xxx,那么第 iii 个木棍有且仅有两种放置方法: 放到 [x+1,x+a[i]][x+ ...

  9. CodeForces - 1551E Fixed Points(dp)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的序列,需要求出删掉最少的数字,使得剩下的数字至少有 kkk 个位置满足 a[i]=ia[i]=ia[i]=i 成立 题目分析:看完数据范围不难想 ...

最新文章

  1. 复化梯形公式,Newton-Cotes公式,变量代换后的复化梯形公式,Gauss-Legendre公式,Gauss-Jacobi公式插值积分的精确度比较
  2. html将字符转成浮点数,stm32用串口接收到数据,怎么把接收到的字符串转换成浮点数?...
  3. 链队列的建立、判空、入队、出队、求长、访头、清空和销毁
  4. TF之Windows:Windows系统下设置Tensorflow运行方式为GPU加速运行的详细攻略
  5. mysql中depart_mysql 学习笔记
  6. Linux中防火墙命令笔记
  7. php global没用,global php 不起作用怎么办
  8. 密码加密模块 bcrypt (详细,好理解,示例源码)
  9. 线性代数 (二): 证明 A^T 乘以 A 为半正定矩阵
  10. 商业的10个最佳Android应用程序模板
  11. 关于Qt bindValue函数出错问题
  12. 安卓Alarm闹钟唤醒耗电问题的排查
  13. 供应链金融及产业风控
  14. Android模拟登录教务系统(强智系统)
  15. Matlab优化工具箱——Optimization Toolbox
  16. 如何增加你微博的粉丝人数?微博推广20绝招
  17. TCP/IP卷一:20---链路层之(环回地址)
  18. MindSpore论坛十月有奖活动
  19. 网站SEO诊断分析要点
  20. android屏幕共享demo,屏幕共享

热门文章

  1. 异常处理-RestFul的异常处理
  2. SpringAMQP--基本介绍
  3. java实现的代理的两种办法
  4. zookeeper的名词复盘-Stat状态信息
  5. MapStruct解决数据传输对象转换的繁琐
  6. spring编程式事务控制
  7. show status用法
  8. finally代码块
  9. SpringBoot 数据验证错误处理
  10. c语言小游戏贪吃神,[原创]自己编的一个贪吃蛇小游戏