当前位置:网站首页>字母交换--字符串dp
字母交换--字符串dp
2022-08-02 10:55:00 【WAWA源】
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <queue>
using namespace std;
#define int long long
const int N = 200010;
int n,k;
string s;
vector<int>vec[30];
int f[1010][1010];
int cal(vector<int> vec)
{
int n=vec.size();
memset(f,0,sizeof f);
for(int i=0;i+1<n;i++)
f[i][i+1]=vec[i+1]-vec[i]-1;
for(int i=2;i<n;i++)
{
for(int j=0;j+i<n;j++)
{
int l=j,r=j+i;
f[l][r]=f[l+1][r-1]+abs(vec[r]-vec[l])-(r-l);
}
}
int res=0;
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(f[i][j]<=k)res=max(j-i+1,res);
return res;
}
signed main()
{
cin>>s>>k;
for(int i=0;i<s.size();i++)
vec[s[i]-'a'].push_back(i);
int res=0;
for(int i=0;i<26;i++)res=max(res,cal(vec[i]));
cout<<res<<'\n';
}
边栏推荐
猜你喜欢
365天挑战LeetCode1000题——Day 047 设计循环队列 循环队列
C#/VB.NET to add more lines more columns image watermark into the Word document
有奖征集|TaoCloud&宝德联合举办全闪POC!
Outsourced Student Management System Architecture Documentation
“纯C”实现——三子棋小游戏
bgp与mpls综合实验
Oracle根据时间查询
MySql模糊查询大全
OLED的HAL库代码介绍及使用(stm32f1/I2C/HAL库版/100%一次点亮)
STM32+MPU6050设计便携式Mini桌面时钟(自动调整时间显示方向)
随机推荐
STM32+MPU6050设计便携式Mini桌面时钟(自动调整时间显示方向)
神通数据库,批量插入数据的时候失败
ES2020-23简单易懂又实用的精选特性讲解 日常开发必备干货!
Geoffery Hinton:深度学习的下一个大事件
LayaBox---TypeScript---高级类型
armv7与armv8的区别(v8和w12的区别)
How to choose a truly "easy-to-use, high-performance" remote control software
程序员的浪漫七夕
8年软件测试工程师的感悟:与薪资相匹配的永远是实力
Oracle降低高水位
MySQL百万数据优化总结 一
Oracle根据时间查询
21天学习挑战赛--第一天打卡(屏幕密度)
如何搭建威纶通触摸屏与S7-200smart之间无线PPI通信?
LayaBox---TypeScript---Mixins
多线程之生产者与消费者
LayaBox---TypeScript---模块解析
3 d laser slam: LeGO - LOAM - ground point extracting method and the analysis of the code
Question about #oracle#, how to solve it?
Geoffery Hinton: The Next Big Thing in Deep Learning