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 ...
随机推荐
- shell 调试 `<<' is not matched
我的这段脚本,验证数据库连接是否正常: #! /bin/sh...while ..do....sqlplus $user/ $passwd@$sid <<!quit;! ... 单独执行 ...
- maven 工程导入jar包
Maven项目引入jar包的方法,希望能帮助有需要的朋友们 法一.手动导入:项目右键—>Build Path—>Configure Build Path—>选中Libraries—& ...
- ABP Zero最新版源码
获取专业版源码 官网 学习版源码
- LN : leetcode 312 Burst Balloons
lc 312 Burst Balloons 312 Burst Balloons Given n balloons, indexed from 0 to n-1. Each balloon is pa ...
- spark on yarn模式下内存资源管理(笔记2)
1.spark 2.2内存占用计算公式 https://blog.csdn.net/lingbo229/article/details/80914283 2.spark on yarn内存分配** 本 ...
- Sass的的使用一
sass -v 检测是否安装 Sass 成功 gem update sass 更新 Sass gem uninstall sass 删除/卸载 Sass 的编译有多种方法: 1.命令编译2.GUI工具 ...
- 无聊的我写了一个代码 。。。P1605 迷宫
搜索水题 哎 直接不行了 . #include <ctype.h> #include <cstdio> void read(int &x) { x=;char ch=g ...
- Android(java)学习笔记199:JNI之JNI概念
1. JNI是什么? java native interface (java本机接口) 比如方法声明: public final native Class<?> getClass(): ...
- [CodeForces]1059D Nature Reserve
大意:给你一个平面上N(N<=100000)个点,问相切于x轴的圆,将所有的点都覆盖的最小半径是多少. 计算几何???Div2的D题就考计算几何???某人昨天上课才和我们说这种计算几何题看见就溜 ...
- 所有的工作目录 都要svn_开头,并且要进行svn同步,你能保证你不删除,你保证不了非你!
所有的工作目录 都要svn_开头,并且要进行svn同步,你能保证你不删除,你保证不了非你! 血的代价啊~