Codeforces913E. Logical Expression
现有串x=11110000,y=11001100,z=10101010,通过这三个串只用与或非三种操作到达给定的串,优先级非>或>与,可以加括号,问表达式最短的里面字典序最小的是谁,有<=10000个询问。
一个串,经过某种变换,到达另一个串,这种转移关系用图论极其合适。那么问题就转化成了一个最短路问题,其中最短包含题目的两个条件。
然而,“某种变换”,即与或非,能否进行,跟优先级息息相关的。这里所关系到的优先级其实指的是最后一次操作的优先级。因此把最后一次操作是谁也列入状态。
然后就划一下优先级,可以发现括号和非同级(3),然后与(2),然后或(1)。这样就可以转移了:>=3级的可以加非,>=2级的可以加与,>=1级的可以加或,而与和或都需要枚举其他所有不低于当前状态优先级的状态来转移,所以复杂度(n^2*log(n)*字符串比较),大约是(n^3)。
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
//#include<assert.h>
#include<algorithm>
#include<string>
#include<queue>
#include<iostream>
using namespace std; int n=,x=,y=,z=; bool ls(const string &a,const string &b)
{
if (a.length()!=b.length()) return a.length()<b.length();
return a<b;
} #define maxn 311
string dis[maxn][];
struct qnode
{
int id,p;
bool operator < (const qnode &b) const {return ls(dis[id][p],dis[b.id][b.p]);}
bool operator > (const qnode &b) const {return ls(dis[b.id][b.p],dis[id][p]);}
};
priority_queue<qnode,vector<qnode>,greater<qnode> > q;
void insert(int a,int b,string s)
{
if (dis[a][b].empty() || ls(s,dis[a][b]))
{
dis[a][b]=s;
q.push((qnode){a,b});
}
}
void dijkstra()
{
insert(x,,"x");
insert(y,,"y");
insert(z,,"z");
while (!q.empty())
{
const qnode now=q.top(); q.pop();
if (now.p==)
{
insert(now.id^(n-),,"!"+dis[now.id][now.p]);
insert(now.id,,dis[now.id][now.p]);
}
else if (now.p==)
{
for (int i=;i<n;i++) if (i!=now.id)
for (int j=;j<=;j++)
if (!dis[i][j].empty())
{
insert(now.id&i,,dis[now.id][]+"&"+dis[i][j]);
insert(now.id&i,,dis[i][j]+"&"+dis[now.id][]);
}
insert(now.id,,dis[now.id][now.p]);
insert(now.id,,"("+dis[now.id][now.p]+")");
}
else
{
for (int i=;i<n;i++) if (i!=now.id)
for (int j=;j<;j++)
if (!dis[i][j].empty())
{
insert(now.id|i,,dis[now.id][]+"|"+dis[i][j]);
insert(now.id|i,,dis[i][j]+"|"+dis[now.id][]);
}
insert(now.id,,"("+dis[now.id][now.p]+")");
}
}
} int T;
int main()
{
dijkstra();
scanf("%d",&T);
while (T--)
{
int now=;
for (int j=,x;j<;j++) scanf("%1d",&x),(x && (now+=(<<j)));
cout<<dis[now][]<<endl;
}
return ;
}
然而由于最长串是非常短的,所以也可以n^2*最长串*字符串比较,即更新到没有状态被更新时退出最短路。
Codeforces913E. Logical Expression的更多相关文章
- 【CodeForces】913 E. Logical Expression
[题目]E. Logical Expression [题意]令x=11110000(2),y=11001100(2),z=10101010(2),n次询问,每次要求用[与][或][非][括号]构成含至 ...
- Codeforces Hello 2018 E题Logical Expression dp+最短路 好题
j题目链接: http://codeforces.com/contest/913/problem/E 题意: 给你x,y,z三个变量,与& 或| 非! 括号() 四种运算符,规定括 ...
- codeforces 931E Logical Expression dp
time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standa ...
- Inside TSQL Querying - Chapter 1. Logical Query Processing
Logical Query Processing Phases Summary (8) SELECT (9) DISTINCT (11) <TOP_specification> <s ...
- [GodLove]Wine93 Tarining Round #7
比赛链接: http://vjudge.net/contest/view.action?cid=47643#overview 比赛来源: 2012 ACM/ICPC Asia Regional Han ...
- Thinking in Java——笔记(3)
Operator Using Java operators Some operators change the value of an operand. This is called a side e ...
- 【ruby】ruby基础知识
Install Ruby(安装) For windows you can download Ruby from http://rubyforge.org/frs/?group_id=167 for L ...
- Oracle迁移MySQL笔记
1,--在oracle代表注释 ,mysql/* */,# 2,|| oracle里面是表示连接符号,比如 A||B 那么就是AB 3,databaseLink创建好之后,比如名字为db_link_b ...
- [转] 編程風格要素-The Elements of Programming Style 中文英文中英對照
转自: http://www.loliman3000.com/tech/2fe33ce32906f0302412881.php 下面的程序風格規則提煉自Brian Kernighan和P. J. Pl ...
随机推荐
- java 字符串截取的几种方式
1.split()+正则表达式来进行截取. 将正则传入split().返回的是一个字符串数组类型.不过通过这种方式截取会有很大的性能损耗,因为分析正则非常耗时. String str = " ...
- jdk 1.8下 java ArrayList 添加元素解析
转载请注明http://www.cnblogs.com/majianming/p/8006452.html 有人问我,java ArrayList底层是怎么实现的?我就回答数组,他再问我,那它是怎么实 ...
- 第一次阅读作业 xinzcover
---恢复内容开始--- 第一次阅读和准备作业 这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1 这个作 ...
- linux下redis安装访问
下载编译安装 wget http://download.redis.io/releases/redis-3.0.1.tar.gz tar xvf redis-3.0.1.tar.gz mv redis ...
- swift 待研备份
https://www.ctolib.com/topics-115290.html 但是还是用到了一个叫做 The Protocol Witness Table (PWT) 的函数表 https:// ...
- 处理不同jQuery版本的兼容性问题
众所周知,jquery版本很多,而且有些版本的冲突也非常明显,有一些网上流传的很实用的插件是用A版本写的,但是要实现另各功能又必須用B版本.所以实现版本之間的和平相处很重要. 1.这里介绍一个函数,可 ...
- DEALLOCATE - 删除一个准备好的查询
SYNOPSIS DEALLOCATE [ PREPARE ] plan_name DESCRIPTION 描述 DEALLOCATE 用于删除前面准备好的查询. 如果你没有明确 DEALLOCATE ...
- cp - 复制文件和目录
总览 cp [选项] 文件路径 cp [选项] 文件...目录 POSIX 选项: [-fipRr] GNU 参数(最短形式): [-abdfilprsuvxPR] [-S SUFFIX] [-V { ...
- Go语言 之md5加密
//方式一 func getMd5String1(str string) string { m := md5.New() _, err := io.WriteString(m, str) if err ...
- Flutter走过的坑(持续更新)
1 Target of URI doesn't exist 'package:flutter/material.dart' 官方下载的flutter中有一个example文件夹,里面有很多flutte ...