现有串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的更多相关文章

  1. 【CodeForces】913 E. Logical Expression

    [题目]E. Logical Expression [题意]令x=11110000(2),y=11001100(2),z=10101010(2),n次询问,每次要求用[与][或][非][括号]构成含至 ...

  2. Codeforces Hello 2018 E题Logical Expression dp+最短路 好题

    j题目链接: http://codeforces.com/contest/913/problem/E 题意: 给你x,y,z三个变量,与&   或|  非!  括号()   四种运算符,规定括 ...

  3. codeforces 931E Logical Expression dp

    time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standa ...

  4. Inside TSQL Querying - Chapter 1. Logical Query Processing

    Logical Query Processing Phases Summary (8) SELECT (9) DISTINCT (11) <TOP_specification> <s ...

  5. [GodLove]Wine93 Tarining Round #7

    比赛链接: http://vjudge.net/contest/view.action?cid=47643#overview 比赛来源: 2012 ACM/ICPC Asia Regional Han ...

  6. Thinking in Java——笔记(3)

    Operator Using Java operators Some operators change the value of an operand. This is called a side e ...

  7. 【ruby】ruby基础知识

    Install Ruby(安装) For windows you can download Ruby from http://rubyforge.org/frs/?group_id=167 for L ...

  8. Oracle迁移MySQL笔记

    1,--在oracle代表注释 ,mysql/* */,# 2,|| oracle里面是表示连接符号,比如 A||B 那么就是AB 3,databaseLink创建好之后,比如名字为db_link_b ...

  9. [转] 編程風格要素-The Elements of Programming Style 中文英文中英對照

    转自: http://www.loliman3000.com/tech/2fe33ce32906f0302412881.php 下面的程序風格規則提煉自Brian Kernighan和P. J. Pl ...

随机推荐

  1. 449 Serialize and Deserialize BST 序列化和反序列化二叉搜索树

    详见:https://leetcode.com/problems/serialize-and-deserialize-bst/description/ C++: /** * Definition fo ...

  2. 202 Happy Number 快乐数

    写一个算法来判断一个数是不是“快乐数”.一个数是不是快乐是这么定义的:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,或是无限循环但始终变不到 1.如 ...

  3. 多个文本框点击复制 zClip (ZeroClipboard)有关问题

    <script type="text/javascript" src="js/jquery.min.js"$amp;>amp;$lt;/script ...

  4. (四)SpringIoc之Bean装配

    在pom.xml的依赖 <dependencies> <!--测试包--> <dependency> <groupId>junit</groupI ...

  5. 读《实战 GUI 产品的自动化测试》之:第四步,高阶技巧

    转自:http://www.ibm.com/developerworks/cn/rational/r-cn-guiautotesting4/ 定义测试控件库 本系列前几篇文章对 IBM 框架做了介绍, ...

  6. Activity的四种启动模式区别

    (1) standard 模式启动模式,每次激活Activity时都会创建Activity,并放入任务栈中. (2) singleTop 如果在任务的栈顶正好存在该Activity的实例, 就重用该实 ...

  7. scss常规用法

    保持sass条理性和可读性的最基本的三个方法:嵌套.导入和注释. 一般情况下,你反复声明一个变量,只有最后一处声明有效且它会覆盖前边的值. $link-color: blue; $link-color ...

  8. thisInObject

    var k = { name:"kName" ,getName:function(){ console.info(this.name) } ,getParentName:funct ...

  9. vue按需加载组件-webpack require.ensure

    使用 vue-cli构建的项目,在 默认情况下 ,执行 npm run build 会将所有的js代码打包为一个整体, 打包位置是 dist/static/js/app.[contenthash].j ...

  10. fastclick.js插件使用

    引入插件步骤 ①在HTML页面中添加  <script type='application/javascript' src='/path/to/fastclick.js'></scr ...