当前位置:网站首页>Hdu2022 多校训练(5) BBQ
Hdu2022 多校训练(5) BBQ
2022-08-06 08:48:00 【ThXe】
题意:
给定一个字符串,可以任意增删改,使其每四个字符为一组都为回文串,问最小操作步数
#include<bits/stdc++.h>
using namespace std;
int t[10];
int g[10][5];// 原长度为g[i][j] 为原长度为i的模式串变为长度为j的模式串的最短编辑距离
int w[3000000];//将模式串映射成一个数字,代表其权值
void dfs(int n, int c, int idx)//预处理每个模式串变为合法子串所需要的消耗。
{
// cout << idx << endl;
int m = 9999999;
if (n)
for (int a = 1; a <= 7; a++)
for (int b = 1; b <= 7; b++)
{
int p[5] = {
0,a,b,b,a };
memset(g, 1, sizeof g);
for (int i = 0; i <= 4; i++)
g[0][i] = i;
for (int i = 0; i <= 7; i++)
g[i][0] = i;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= 4; j++)
g[i][j] = std::min(std::min(g[i - 1][j] + 1, g[i - 1][j - 1] + (t[i] != p[j])), g[i][j - 1] + 1);
if (g[n][4] < m)m = g[n][4];
}
if (n)w[idx] = m;
if (n == 7)return;
n++;
for (int i = 1; i <= c; i++)
{
t[n] = i;
dfs(n, c, idx * 10+ i);
}
t[n] = c + 1;
dfs(n, c + 1, idx * 10 + c + 1);
}
char s[1000001];
int dp[1000001];// dp[i]为当前长度为i变成合法条件的最小步数
int last[26];
int pre[1000001];//每个字符出现的上一个位置
void sol()
{
scanf("%s", s + 1);
int n = strlen(s + 1);
memset(dp + 1, 10, n * 4);
memset(last, -1, sizeof last);
for (int i = 1; i <= n; i++)//映射方法
{
pre[i] = last[s[i] - 'a'];
last[s[i] - 'a'] = i;
}
for (int i = 0; i < n; i++)
{
dp[i + 1] = std::min(dp[i + 1], dp[i] + 1);
int cnt = 0;int idx = 0; int tmp[8];
for (int j = 1; j <= 7; j++)tmp[j] = 0;
for (int j = 1; j <= 7 && i + j <= n; j++)
{
//int c = s[i + j] - 'a';
if (pre[i + j] <= i)//在上一个出现的位置是在前i个,说明是一个新的字母
idx = idx * 10 + (tmp[j] = ++cnt);
else//当前字符等于之前的字符的编码
idx = idx * 10 + (tmp[j] = tmp[pre[i + j] - i]);//将当前串变为模式串并转换成相应下标值。
dp[i + j] = std::min(dp[i + j], dp[i] + w[idx]);
}
}
printf("%d\n", dp[n]);
}
int main()
{
dfs(0, 0, 0);
int t;
scanf("%d", &t);
while (t--)sol();
}
边栏推荐
- About the third parameter of np.zeros(): c represents similar to c language, row priority; F represents column priority record
- VS同一解决方案的不同项目的命名空间名字唯一
- Can the code signing certificate solve the software being alerted by antivirus software?
- 剑指 Offer 33. 二叉搜索树的后序遍历序列
- 流體小球加載動畫
- C语言 结构体
- [Cloud Native--Kubernetes] Configuration Management
- 山石发声 | 做好安全运营,没有你想象的那么难
- MySQL数据库的数据类型和基于MySQL数据类型的综合实例项目
- UNIX环境高级编程-第一章
猜你喜欢

第十七天(续第十六天BPDU相关知识以及STP的配置)

华为外包测试2年,不甘被替换,168天的学习转岗成正式员工

CPU Architecture at a Glance

Jetpack WorkManager 看这一篇就够了~

How to limit command length to bounce shell

第十九章 自动化理论

代码签名证书可以解决软件被杀毒软件报毒提醒吗?

Datax3.0+DataX-Web打造分布式可视化ETL系统

《微信小程序-进阶篇》Lin-ui组件库源码分析-动画组件Transition(一)

UNIX environment advanced programming - the first chapter
随机推荐
中国电子学会青少年等级考试五级原题
凹语言——名字的由来和寓意
Summary of the experience of project operation and maintenance work
下雨小雲動畫
[图]Edge 104稳定版发布:引入“增强安全模式”
Parameter ‘courseId‘ not found. Available parameters are [arg1, arg0, param1, para
Linux——MySQL安装的几种方式
90%的软件测试从业者,努力的方向都错了...你呢?
how to jump higher
【云原生--Kubernetes】配置管理
About the third parameter of np.zeros(): c represents similar to c language, row priority; F represents column priority record
Use Specification and Example to implement dynamic conditional query cases
代码签名证书可以解决软件被杀毒软件报毒提醒吗?
Parameter ‘courseId’ not found. Available parameters are [arg1, arg0, param1, para
2022海亮SC游记
Simulate the realization of strcpy function (including multiple optimization ideas)
关于np.zeros()第三个参数:c代表与c语言类似,行优先;F代表列优先的记录
海外媒体知名媒体宣发:欧美媒体发稿的基本流程
ROS报错[rospack] Error: package ‘.....‘ not found
不会吧,不会吧都2022年了你不会还不知道Jmeter原理吧