【最短路】【Heap-dijkstra】hihocoder 1587 ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 J. Typist's Problem
题意:给你一个串,仅含有a~g,且每个字母只出现最多一次。和一个光标初始位置,以及一个目标串,问你最少要多少的代价变化成目标串。
有五种操作:在光标前添加一个未出现过的字母,代价1。
删除光标前或者光标后的字母,代价1。
光标左移或者右移,代价0.5。
哈希,把串弄成一个八进制数,加上一个光标位置,状态数不超过8^8。
直接跑dijkstra即可。
要注意初始化的时候,可以单独记一个数组,表示用过的状态,仅仅重置这些状态,防止初始化复杂度过高。
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
char s1[10],s2[10];
int pp,len1,len2,d[17000000],pw[10];
bool vis[17000000],cant[8];
int st[17000000],En;
struct Node{
int u,d;
Node(const int &u,const int &d){
this->u=u;
this->d=d;
}
Node(){}
};
bool operator < (const Node &a,const Node &b){
return a.d>b.d;
}
priority_queue<Node>Heap;
int main(){
// freopen("j.in","r",stdin);
memset(d,0x7f,sizeof(d));
pw[0]=1;
for(int i=1;i<=8;++i){
pw[i]=pw[i-1]*8;
}
while(scanf("%s%d%s",s1,&pp,s2)!=EOF){
memset(cant,0,sizeof(cant));
bool flag=1;
while(!Heap.empty()){
Heap.pop();
}
En=0;
int U=0,len1=strlen(s1),len2=strlen(s2);
for(int i=0;i<len2;++i){
if(cant[s2[i]-'a'+1]){
flag=0;
break;
}
cant[s2[i]-'a'+1]=1;
}
if(!flag){
puts("-1");
continue;
}
for(int i=0;i<len1;++i){
U=U*8+s1[i]-'a'+1;
}
U=U*8+pp;
int goal=0;
for(int i=0;i<len2;++i){
goal=goal*8+s2[i]-'a'+1;
}
Heap.push(Node(U,0));
d[U]=0;
st[++En]=U;
while(!Heap.empty()){
Node now=Heap.top(); Heap.pop();
if(!vis[now.u]){
if(now.u/8==goal){
break;
}
vis[now.u]=1;
int U=now.u;
int len=0;
int gbp=U%8; U/=8;
memset(cant,0,sizeof(cant));
// char S[10];
while(U){
cant[U%8]=1;
// S[len++]=U%8+'a'-1;
++len;
U/=8;
}
// for(int i=0;i<len;++i){
// putchar(S[i]);
// }
// puts("");
//plus
if(len<7){
U=now.u;
U-=(U%pw[len-gbp+1]);
U*=8;
U+=(now.u%pw[len-gbp+1]);
for(int i=1;i<=7;++i){
if(!cant[i]){
int tU=U+i*pw[len-gbp+1];
++tU;
if(d[tU]>d[now.u]+2){
d[tU]=d[now.u]+2;
Heap.push(Node(tU,d[tU]));
st[++En]=tU;
}
}
}
}
//delete
if(len>0){
if(gbp>0){
U=now.u%pw[len-gbp+1];
int tmp=now.u;
tmp/=pw[len-gbp+2];
tmp*=pw[len-gbp+1];
U+=tmp;
--U;
if(d[U]>d[now.u]+2){
d[U]=d[now.u]+2;
Heap.push(Node(U,d[U]));
st[++En]=U;
}
}
if(gbp<len){
U=now.u%pw[len-gbp];
int tmp=now.u;
tmp/=pw[len-gbp+1];
tmp*=pw[len-gbp];
U+=tmp;
if(d[U]>d[now.u]+2){
d[U]=d[now.u]+2;
Heap.push(Node(U,d[U]));
st[++En]=U;
}
}
}
//move
if(gbp>0){
if(d[now.u-1]>d[now.u]+1){
d[now.u-1]=d[now.u]+1;
Heap.push(Node(now.u-1,d[now.u-1]));
st[++En]=now.u-1;
}
}
if(gbp<len){
if(d[now.u+1]>d[now.u]+1){
d[now.u+1]=d[now.u]+1;
Heap.push(Node(now.u+1,d[now.u+1]));
st[++En]=now.u+1;
}
}
}
}
printf("%.1f\n",(double)(*min_element(d+goal*8,d+goal*8+8))*0.5);
for(int i=1;i<=En;++i){
d[st[i]]=2000000000;
vis[st[i]]=0;
}
}
return 0;
}
【最短路】【Heap-dijkstra】hihocoder 1587 ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 J. Typist's Problem的更多相关文章
- hihoCoder 1582 Territorial Dispute 【凸包】(ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)
#1582 : Territorial Dispute 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In 2333, the C++ Empire and the Ja ...
- hihoCoder 1584 Bounce 【数学规律】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)
#1584 : Bounce 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 For Argo, it is very interesting watching a cir ...
- hihoCoder 1578 Visiting Peking University 【贪心】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)
#1578 : Visiting Peking University 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Ming is going to travel for ...
- hihoCoder 1586 Minimum 【线段树】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)
#1586 : Minimum 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 You are given a list of integers a0, a1, …, a2 ...
- hihocoder 1586 ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛-题目9 : Minimum【线段树】
https://hihocoder.com/problemset/problem/1586 线段树操作,原来题并不难..... 当时忽略了一个重要问题,就是ax*ay要最小时,x.y可以相等,那就简单 ...
- 【分类讨论】【计算几何】【凸包】hihocoder 1582 ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 E. Territorial Dispute
题意:平面上n个点,问你是否存在一种黑白染色方案,使得对于该方案,无法使用一条直线使得黑色点划分在直线一侧,白色点划分在另一侧.如果存在,输出一种方案. 如果n<=2,显然不存在. 如果所有点共 ...
- 【线段树】hihocoder 1586 ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 I. Minimum
题意:给你一个序列(长度不超过2^17),支持两种操作:单点修改:询问区间中最小的ai*aj是多少(i可以等于j). 只需要线段树维护区间最小值和最大值,如果最小值大于等于0,那答案就是minv*mi ...
- hihoCoder #1586 : Minimum-结构体版线段树(单点更新+区间最值求区间两数最小乘积) (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)
#1586 : Minimum Time Limit:1000ms Case Time Limit:1000ms Memory Limit:256MB Description You are give ...
- hihoCoder 1389 Sewage Treatment 【二分+网络流+优化】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)
#1389 : Sewage Treatment 时间限制:2000ms 单点时限:2000ms 内存限制:256MB 描述 After years of suffering, people coul ...
随机推荐
- delphi按钮文字换行
delphi按钮有TButton和TBitButton,而TButton不支持换行,TBitButton支持 拖拽TBitButton按钮以后,按alt+F12进入找到TBitButton的capti ...
- beego 相关
bee api bapi bee run -downdoc=true -docgen=true
- Python模块学习 - Fileinput
Fileinput模块 fileinput是python提供的标准库,使用fileinput模块可以依次读取命令行参数中给出的多个文件.也就是说,它可以遍历 sys.argv[1:],并按行读取列表中 ...
- Linux 入门记录:一、命令行 Bash 的基本操作
为了以后长期的线上测试和服务器的性能考量,要用 Linux 服务器了.昨晚装了个 CentOS 6.9,今天开始学学 Linux 基础,扫扫盲.ok,小本本记 ing... 一.Shell简介 She ...
- 5-3 Linux内核计时、延时函数与内核定时器【转】
转自:http://www.xuebuyuan.com/510594.html 5-3 Linux内核计时.延时函数与内核定时器 计时 1. 内核时钟 1.1 内核通过定时器(timer)中断来跟 ...
- 64_l2
libcec-devel-4.0.2-3.fc26.x86_64.rpm 22-May-2017 22:13 27942 libcephfs-devel-10.2.7-2.fc26.i686.rpm ...
- Educational Codeforces Round 23 补题小结
昨晚听说有教做人场,去补了下玩. 大概我的水平能做个5/6的样子? (不会二进制Trie啊,我真菜) A. 傻逼题.大概可以看成向量加法,判断下就好了. #include<iostream> ...
- problems when installed mysql in linux ubuntu
reference:http://www.jb51.net/article/87160.htm?pc 1.ERROR 2002 (HY000): Can't connect to local MySQ ...
- openstack前期准备
. 两台虚拟机,安装Centos7系统 两个网卡 -- 一个NAT模式,一个仅主机模式 两个硬盘 -- 一个20GB,一个50GB 内存 -- 主 .6GB(根据自己的配置,大于2G即可) 从 1.6 ...
- 用JavaScript校验日期的合法性
校验表单时可能会遇到校验日期是否正确.可以利用JS的内置对象Date帮助我们完成日期校验. 思路是首先用被校验日期(假设为A,可能为字符串或数字)创建一个Date对象(假设为B). 然后判断A和B的年 ...