SCUT125 华为杯 D.笔芯回文 —— DP
来源:互联网 发布:it职业技术学校 编辑:程序博客网 时间:2024/04/28 21:56
题目链接: https://scut.online/p/125
题解:
1.ok[l][r]代表区间l~r的子串是否为回文串,O(n^2)预处理。
2. dp[i]代表删除前i个字符的最大价值, 状态转移方程为:if(ok[j][i]) dp[i] = max(dp[i],dp[j-1]+a[i-j+1]);
代码如下:
#include <bits/stdc++.h>using namespace std;#define ms(a, b) memset((a), (b), sizeof(a))typedef long long LL;const int INF = 2e9;const LL LNF = 9e18;const int mod = 1e9+7;const int maxn = 5000+10;char s[maxn];LL a[maxn], dp[maxn];bool ok[maxn][maxn];int n, len;void init(){ ms(ok,0); ms(dp,0); for(int i = 1; i<=len; i++) { int l = i, r = i; while(l>=1 && r<=len && (r-l+1)<=n && s[l]==s[r]) ok[l--][r++] = 1; } for(int i = 1; i<=len; i++) { int l = i, r = i+1; while(l>=1 && r<=len && (r-l+1)<=n && s[l]==s[r]) ok[l--][r++] = 1; }}void solve(){ for(int i = 1; i<=len; i++) for(int j = 1; j<=i; j++) { if(ok[j][i]) dp[i] = max(dp[i],dp[j-1]+a[i-j+1]); } printf("%lld\n",dp[len]);}int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i = 1; i<=n; i++) scanf("%lld",&a[i]); scanf("%s", s+1); len = strlen(s+1); init(); solve(); } return 0;}
0 0
- SCUT125 华为杯 D.笔芯回文 —— DP
- SCUT129 笔芯值 —— DP or 容斥原理
- Codeforces 835D Round #427 D-Palindromic characteristics—— DP+回文串
- 华为机试——回文判断
- 华为机试—回文字符串
- 华为机试—回文数判断
- 华为机试—回文字符串
- 华为机试—回文字符串
- dp——洛谷 P1435 回文字串
- 华为机试—鉴定回文数—较少code
- 华为机试——回文数判断
- 华为机试—最长回文子串
- 华为2017年大牛机试题—最长回文字符串
- CodeForces 159D Palindrome pairs (回文子串、dp)
- Codeforces 835D Round #427 Div2D :回文串DP
- DP入门50题(7)——NYoj37 回文字符串
- 简单dp算法——百炼05:切割回文
- 51NOD1092—— 回文字符串(区间dp)
- 浅谈腾讯热更新开源框架xLua自定义Loader
- linux下解压命令大全
- iOS
- 1051. 复数乘法
- loadrunner中lr_wasted_time函数
- SCUT125 华为杯 D.笔芯回文 —— DP
- ZigZag Conversion
- lightoj 1205
- error C2589: “(”:“::”右边的非法标记
- Android app 安全测试调研及执行
- Java IO流之对象流
- JavaScript常用工具函数(验证数据格式、清除空格、判断浏览器类型、cookie操作等)
- Android中判断网络连接是否可用及监控网络状态
- volatile的java弱一致性(ConcurrentHashMap 中get 是不加锁的)后补