[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指针,用于本组交换后尾指针在下一 ...
随机推荐
- vue 新鼠标移入移出事件
@mouseover 鼠标移入 @mouseleave 鼠标移出
- arcpy获取polygon内环
当使用arcpy获取polygon几何的时候,不能像ao一样获取到内外环,只能获取到单个部件.而part返回的即是一个点组了. 所以只能通过None对象进行分割,确定部件内的内外环.一个part内,只 ...
- docker compose的安装
1,安装docker ### CentOS8 默认是会读取centos.org的mirrorlist的,所以一般来说是不需要配置镜像的. # step 1: 安装必要的一些系统工具 sudo yum ...
- Flutter获取当前路由信息和全局路由监听
Flutter获取当前路由信息和全局路由监听 获取当前路由名 通过Flutter提供的方式 var routePath = ModalRoute.of(context).settings.name; ...
- 转载 Spring boot中配置事务管理
一.注解的方式 1. 在Spring boot工程的主入口类中加入注解 // 开启事务支持 @EnableTransactionManagement 1 2 2. 在需要事务支持的服务类(class) ...
- checker jenkins 启动配置
chmod -R 755 bin scp target/*.jar ubuntu@x:/home/ubuntu/checker/ scp config/*.yml ubuntu@x:/home/ubu ...
- Error: Assertion failed (nimages > 0) in cv::calibrateCameraRO, file D:\opencv4\opencv\opencv-4.1.0\modules\calib3d\src\calibration.cpp, line 3691
报错信息: Error: Assertion failed (nimages > 0) in cv::calibrateCameraRO, file D:\opencv4\opencv\open ...
- 揭秘vivo百亿级厂商消息推送平台的高可用技术实践
本文由vivo 互联网服务器团队Yu Quan分享,本文收录时有内容修订和重新排版. 1.引言 如今,Android端的即时通讯IM这类应用想实现离线消息推送,难度越来越大(详见<Android ...
- milvus操作
java 引入依赖 <dependency> <groupId>io.milvus</groupId> <artifactId>milvus-sdk-j ...
- 微信小程序开发基础详解
1.结构 util.js 工具类 app.js 全局工具函数 app.json 小程序配置 app.wxss 全局样式 2.生命周期 onLoad(opt ...