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 ...
随机推荐
- Hadoop集群搭建及MapReduce应用
一.Hadoop集群的搭建与配置 1.节点准备 集群规划: 主机名 IP 安装的软件 运行的进程 weekend 01 192.168.1.60 jdk.hadoop NameNode.DFSZKFa ...
- 【RSA】在 ASP.NET Core中结合web前端JsEncrypt.JS使用公钥加密,.NET Core使用私钥解密;
有一个需求,前端web使用的是JsEncrypt把后端给的公钥对密码进行加密,然后后端对其进行解密: 使用的类库如下: 后端使用第三方开源类库Bouncy Castle进行RSA的加解密和生成PEM格 ...
- Mac 为啥不显示图片尺寸,点了显示简介也不显示~???
这个问题困扰我好几天,然后今天想法子解决,我这个强迫症患者是真的难受,不能直接一目了然的,每次都要ps打开图片去看,真的好心累???? 网上98%的解决方法如下: 在 Finder 中,按快捷键 co ...
- Android EventBus3.0详解
修改日志 -- 添加索引部分得细节,添加kotlin的支持方式 https://www.jianshu.com/p/31e3528ca7e5
- e.Row.RowType == DataControlRowType.DataRow详解(转)
代码语句如下: protected void OnRowCreate(object sender, GridViewRowEventArgs e) { if (e.Row.RowT ...
- iOS Programming UIGestureRecognizer and UIMenuController
iOS Programming UIGestureRecognizer and UIMenuController A UIGestureRecognizer intercepts touches ...
- linux下redis安装访问
下载编译安装 wget http://download.redis.io/releases/redis-3.0.1.tar.gz tar xvf redis-3.0.1.tar.gz mv redis ...
- [转] 以超级管理员身份运行bat
(转自:以超级管理员身份运行bat - lishirong 原文日期:2013.07.04) 废话不多说,直接上代码: -------------------------------------- ...
- zabbix监控之grafana
zabbix监控之grafana
- 让xamarin的Entry绑定时,支持Nullable类型
xamarin.forms默认情况下,如果属性是double?类型,绑定到Entry上,是无法实现双向绑定的, 可以自定义Converter实现双向绑定 public class NullableCo ...