[ABC227E] Swap 题解
考试一道题题解。
30pts:枚举所有串,\(check\) 是否可行。
60pts:做 \(bfs\),暴力推演变化过程,用 \(map\) 去重,时间复杂度 \(O(nC_{n}^{n/3}C_{n-n/3}^{n/3})\)。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
unordered_map<string,int>mp;
string s;int k;
ll ans=1;
struct zjy{
string s;int b;
};queue<zjy>q;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>s>>k;mp[s]=1;
q.push({s,0});
while(q.size()){
int b=q.front().b+1;
s=q.front().s;q.pop();
if(b>k) continue;
for(int i=1;s[i];i++){
swap(s[i-1],s[i]);
if(!mp[s])
mp[s]=1,q.push({s,b}),ans++;
swap(s[i-1],s[i]);
}
}cout<<ans;
return 0;
}
100pts:
考虑到同种字符相对位置不可能发生改变,所以可以直接一个一个加入字符,然后加上将他移动到这里的代价。
设 \(dp_{i,j,k,l}\) 表示前 \(i+j+k\) 位用了 \(i\) 个 \(K\),\(j\) 个 \(E\),\(k\) 个 \(Y\),转换了 \(l\) 次时的可能性。
转移方程详见代码,时间复杂度 \(O(n^5)\)(\(K\) 超过 \(\frac{sz(sz-1)}{2}\) 就是在走倒退)。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
string s;int k,n,m,y;
int a[35],b[35],c[35];
ll dp[35][35][35][605],ans;
int sm1[35],sm2[35],sm3[35];
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>s>>k;int sz=s.size();
k=min(k,sz*(sz-1)/2);
for(int i=0;s[i];i++){
sm1[i+1]=sm1[i];
sm2[i+1]=sm2[i];
sm3[i+1]=sm3[i];
if(s[i]=='K')
a[++n]=i+1,sm1[i+1]++;
if(s[i]=='E')
b[++m]=i+1,sm2[i+1]++;
if(s[i]=='Y')
c[++y]=i+1,sm3[i+1]++;
}dp[0][0][0][0]=1;
for(int i=0;i<=n;i++)
for(int j=0;j<=m;j++)
for(int l=0;l<=y;l++)
for(int t=0;t<=k;t++){
if(i<n){
int p=a[i+1],cnt=max(0,sm2[p]-j)+max(0,sm3[p]-l);
//cnt指将第i+1个K移动到第i+j+l+1位所用的代价,下同
dp[i+1][j][l][t+cnt]+=dp[i][j][l][t];
}if(j<m){
int p=b[j+1],cnt=max(0,sm1[p]-i)+max(0,sm3[p]-l);
dp[i][j+1][l][t+cnt]+=dp[i][j][l][t];
}if(l<y){
int p=c[l+1],cnt=max(0,sm1[p]-i)+max(0,sm2[p]-j);
dp[i][j][l+1][t+cnt]+=dp[i][j][l][t];
}if(i+j+l==sz) ans+=dp[i][j][l][t];
}
cout<<ans;
return 0;
}
[ABC227E] Swap 题解的更多相关文章
- CF1082B Vova and Trophies 题解
CF1082B Vova and Trophies 题解 瞎搞题,推荐的,一看是道水题,就随手A了-- 题目描述 Vova has won \(n\)trophies in different com ...
- 算法与数据结构基础 - 贪心(Greedy)
贪心基础 贪心(Greedy)常用于解决最优问题,以期通过某种策略获得一系列局部最优解.从而求得整体最优解. 贪心从局部最优角度考虑,只适用于具备无后效性的问题,即某个状态以前的过程不影响以后的状态. ...
- [LeetCode 题解]:Swap Nodes in Pairs
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a li ...
- leetcode 题解 || Swap Nodes in Pairs 问题
problem: Given a linked list, swap every two adjacent nodes and return its head. For example, Given ...
- [LeetCode]Swap Nodes in Pairs题解
Swap Nodes in Pairs: Given a linked list, swap every two adjacent nodes and return its head. For exa ...
- 题解 SP27102/UVA1747 【Swap Space】
SP27102 [Swap Space] 双倍经验:UVA1747 Swap Space 用(a,b)表示每个硬盘的原容量和新文件系统下的容量.分两种情况考虑:a≤b和a>b 第一类a≤b格式化 ...
- Swap Nodes in Pairs LeetCode题解
做完这个题目,感觉LeetCode的题目出的真好... 这种题,如果让我在面试时候纸上写代码,肯定会挂的. 我昨天晚上看的题目,昨天脑子是懵的,放下了.今天早上来做. 一开始做,提交,果然错了.写的代 ...
- LeetCode题解之Swap Nodes in Pairs
1.题目描述 2.问题分析 对两个节点进行交换操作 3.代码 ListNode* swapPairs(ListNode* head) { if( !head || head->next == N ...
- PAT甲题题解-1067. Sort with Swap(0,*) (25)-贪心算法
贪心算法 次数最少的方法,即:1.每次都将0与应该放置在0位置的数字交换即可.2.如果0处在自己位置上,那么随便与一个不处在自己位置上的数交换,重复上一步即可.拿样例举例: 0 1 2 3 4 5 ...
- leetcode个人题解——#24 Swap Nodes in Pairs
因为不太熟悉链表操作,所以解决方法烦了点,空间时间多有冗余. 代码中l,r分别是每一组的需要交换的左右指针,temp是下一组的头指针,用于交换后链接:res是交换后的l指针,用于本组交换后尾指针在下一 ...
随机推荐
- MySql 9 in Docker 主从切换
继上一篇<MySql 9 in Docker 利用克隆插件搭建主从>我们说了主从复制后, 那么我们接下来说说如何手动的进行主从切换. 动手~ 1. 原主库设置 切断应用对主库的访问 主库设 ...
- 中电金信:数字经济时代,AI+金融技术应用与未来发展
- Redis应用—9.简单应用汇总
大纲 1.基于Redis实现的简单缓存机制(String数据结构) 2.实现一个最简单的分布式锁(String数据结构) 3.博客网站的文章发布与查看(String数据结构) 4.博客字数统计与文章预 ...
- jdk安装-windows和linux
下载:见此博客https://www.cnblogs.com/zn19961006/p/12857930.html 一.windows安装 1.很简单,运行exe,然后一直下一步 选安装路径. 注意: ...
- 【Python】conda基本使用、pip换源、pip超时问题解决
conda问题 重要警告:安装conda的时候,安装目录不要包含空格以及特殊字符,最好不要直接装在C盘根目录, 往期笔记 conda安装: https://www.cnblogs.com/mllt/p ...
- 黑苹果 - 搭建python自动化测试环境
通用环境 1. 安装 xcode 从 AppStore 安装 安装完成之后,打开 xcode,同意各种协议 不用新建项目 注意: xcode下载完成后,安装的过程很慢,需要等待.我是12.5版本,差不 ...
- gitlab runner install
gitlab-runner install -d /home/gitlab-runner/ --syslog --user gitlab-runner
- 在Ubuntu系统上手动安装GCC环境
Ubuntu系统是自带GCC安装指令的apt install gcc,当前apt源中gcc版本为5.4.0,版本太低,推荐手动安装gcc8.3.0 手动安装gcc8.3.0之前需要先确保安装gcc环境 ...
- linux shell移植,sh不支持数组及bash移植
查看此时系统shell ls -al /bin/sh Linux 操作系统缺省的 shell 是Bourne Again shell,它是 Bourne shell 的扩展,简称 Bash,与 Bou ...
- hive表元数据读取不到
MetaException(message:java.lang.UnsupportedOperationException: Storage schema reading not supported) ...