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 ...
随机推荐
- js ajax 数组类型参数传递
若一个请求中包含多个值,如:(test.action?tid=1&tid=2&tid=3),参数都是同一个,只是指定多个值,这样请求时后台会发生解析错误,应先使用 tradititon ...
- bootstrap datatable项目封装
(function($) { $.fn.formJSON = function() { var serializeObj = {}; var array = t ...
- css靠左,靠右
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 7.JAVA-类继承、覆写、final关键字
1.JAVA继承-extends 在java中,要想实现继承则使用extends关键字. 一般子类被称为派生类,父类称为基类(super) extends需要注意的地方: java不允许多重继承(一个 ...
- [BZOJ1878][SDOI2009]HH的项链 莫队
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1878 不带修改的莫队,用一个桶记录一下当前区间中每种颜色的数量就可以做到$O(1)$更新了 ...
- iOS Programming UINavigationController
iOS Programming UINavigationController the Settings application has multiple related screens of info ...
- R Programming week 3-Loop functions
Looping on the Command Line Writing for, while loops is useful when programming but not particularly ...
- iOS----创建静态库
静态库 1.什么是库? 库是程序代码的集合,是共享程序代码的一种方式 2.根据源代码的公开情况,库可以分为2种类型 开源库 公开源代码,能看到具体实现 比如SDWebImage.AFNetworkin ...
- Python+CGI,在Windows上快速部署Python到IIS
通过CGI,我们可以快速在Windows上部署Python 1. Windows安装IIS服务 2. 在IIS里打开“ISAPI和CGI限制”->添加,路径=python.exe的完全路径+&q ...
- 「Python调试器」,快速定位各种疑难杂症!!
现在很多的编辑器其实都带着「调试程序」的功能,比如写 c/c++ 的 codeblocks,写 Python 的 pycharm,这种图形界面的使用和显示都相当友好,简单方便易学,这个不是我这篇文章要 ...