uva12219 Common Subexpression Elimination

2023-11-07 20:38

本文主要是介绍uva12219 Common Subexpression Elimination,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Let the set consist of all words composed of 1-4 lower case letters,
such as the words \ a “, \ b “, \ f “, \ aa “, \ fun ” and \ kvqf “.
Consider expressions according to the grammar with the two rules E ! f
E ! f ( E;E ) for every symbol f 2 . Any expression can easily be
represented as a tree according to its syntax. For example, the
expression \ a(b(f(a,a),b(f(a,a),f)),f(b(f(a,a),b(f(a,a),f)),f)) ” is
represented by the tree on the left in the following gure: Last night
you dreamt of a great invention which considerably reduces the size of
the representation: use a graph instead of a tree, to share common
subexpressions. For example, the expression above can be represented
by the graph on the right in the gure. While the tree contains 21
nodes, the graph just contains 7 nodes. Since the tree on the left in
the gure is also a graph, the representation using graphs is not
necessarily unique. Given an expression, nd a graph representing the
expression with as few nodes as possible! Input The rst line of the
input contains the number c (1 c 200), the number of expressions.
Each of the following c lines contains an expression according to the
given syntax, without any whitespace. Its tree representation contains
at most 50 000 nodes. Output For each expression, print a single line
containing a graph representation with as few nodes as possible. The
graph representation is written down as a string by replacing the
appropriate subexpressions with numbers. Each number points to the
root node of the subexpression which should be inserted at that
position. Nodes are numbered sequentially, starting with 1; this
numbering includes just the nodes of the graph (not those which have
been replaced by numbers). Numbers must point to nodes written down
before (no forward pointers). For our example, we obtain `
a(b(f(a,4),b(3,f)),f(2,6)) ‘

通过dfs进行建树和去重,关键是判断两棵树是否完全相同。可以对子树进行编号,每个三元组{自身的字符串(的哈希值),左子树编号,右子树编号}对应唯一的编号,在map里进行查询。
注意时限卡的比较紧,建树要做到O(n)。

#include<cstdio>
#include<cstring>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<iostream>
using namespace std;
#define LL unsigned long long
const int pp=131;
struct st
{char s[5];int u,v,l;LL h;bool operator < (const st ss) const{int i;if (h!=ss.h) return h<ss.h;if (u!=ss.u) return u<ss.u;return v<ss.v;}
}a[1000000];
map<st,int> mp;
char s[500000];
int len[1000000],num[1000000],ord[1000000],last[1000000],tot,l,r,cnt,clo,now,K;
bool b[1000000];
int build()
{int i,j,k,cnt,q;bool flag=0;st tem;tem.l=tem.h=0;while (now<=l&&(s[now]!=','&&s[now]!='('&&s[now]!=')')){tem.s[++tem.l]=s[now];tem.h=tem.h*pp+s[now];now++;}tem.s[tem.l+1]=0;if (s[now]=='('){now++;tem.u=build();now++;tem.v=build();now++;}else tem.u=tem.v=0;if (!mp.count(tem)){a[++tot]=tem;mp[tem]=tot;}return mp[tem];
}
void prt(int p)
{if (last[p]<K){last[p]=K;ord[p]=++clo;printf("%s",a[p].s+1);if (!a[p].u) return;printf("(");prt(a[p].u);printf(",");prt(a[p].v);printf(")");}else printf("%d",ord[p]);
}
int main()
{int T;scanf("%d",&T);while (T--){K++;scanf("%s",s+1);l=strlen(s+1);tot=0;now=1;clo=0;cnt=0;mp.clear();prt(build());printf("\n");}
}

这篇关于uva12219 Common Subexpression Elimination的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/366149

相关文章

兔子--Android Studio出现错误:Error:Execution failed for task ':myapp:dexDebug'. com.android.ide.common.pro

重点在:finished with non-zero exit value 2. 这里表明了有重复的内容存在。 由于:Android Studio中引入包的方式有如下2种:    compile 'com.android.support:support-v4:22.0.0'    compile files('libs/support-v

YOLOV5入门教学-common.py文件

在 YOLOv5 框架中,common.py 文件是一个核心组件,负责定义深度学习模型的基础模块和常用操作。无论是卷积层、激活函数、特征融合还是其他复杂的模型结构,common.py 都提供了灵活且高效的实现。在这篇文章中,我们将深入解析 common.py 的设计思想、各个模块的功能以及它在 YOLOv5 中的应用。通过理解该文件的实现细节,不仅可以帮助我们更好地掌握 YOLOv5 的内部结构,

【HDU】4975 A simple Gaussian elimination problem. 网络流——行列建边

传送门:【HDU】4975 A simple Gaussian elimination problem. 题目分析:这题和某一场的多校的题目出奇的像啊!重要的是我一开始还以为不可能会出一样的题。。结果迟迟没写啊。。。后来觉得实在想不出什么对策了,虽然觉得给的是0~9很特殊,但是利用不起来,果断还是敲了网络流了。。首先建图很简单,源点向行建边,容量为行和,列向汇点建边,容量为列和,然后所有的

网络协议栈学习之socket, sock_common, sock, 和 sk_buff

一. 前言   一直很好奇socket是如何实现的,底层的数据结构又是如何,因此在这里对socket的数据结构进行分析。   socket是传输层使用的数据结构,用于声明、定义套接字,网络层会调用sock结构体,其中sock会用到了通用sock_common结构体。而sk_buff则是内核中使用的套接字缓冲区结构体。在我们前文提到的NAT转换中,除了修改内核已有的Netfilter源码外,还有一

Netfilter学习之NAT类型动态配置(八)nf_nat_proto_common.c代码解析

nf_nat_proto_common.c实现了对称型的端口改变,在此我决定对其代码进行分析,以便实现对对称型NAT的随意改动。    具体代码如下: #include <linux/types.h>#include <linux/random.h>#include <linux/netfilter.h>#include <linux/export.h>#include <net/n

Binary Tree - Lowest Common Ancestor 题型总结

Lowest Common Ancestor of a Binary Search Tree 思路:这题跟 Lowest Common Ancestor of Binary Tree 一模一样。思路:就是找p的节点在不在左支,或者右支,找到各自左右节点,然后进行比较,如果两者不一样,说明当前的root就是lowest 父节点,如果左边为空,那就都在右边,返回右边的即可,如果右边为空,那就都在左

leetcode --- Longest Common Prefix

最长公共前缀(Longest  Common  Prefix) 题目:Write a function to find the longest common prefix string amongst an array of strings. 题目链接:https://leetcode.com/problems/longest-common-pref

spring项目common设计

1、策略模式使用,通过一个执行器来分别调用不同的策略类 2、观察者模式,通常使用消息队列来实现,一个生产消息一个监听消息 3、单例模式,通常spring 容器中的bean为单例类,所以大部分单例场景被bean替代了,如配置文件读取 4、AOP对事物、日志、安全和异常处理的切面处理,通常使用spring AOP来实现 5、多线程,通常使用线程池executors 6、定时任务,通常使用spring

MySQL8.0新特性CTE(Common Table Expression)

CTE(Common Table Expression)可以认为是派生表(derived table)的替代,在一定程度上,CTE简化了复杂的join查询和子查询,提高了SQL的可读性和执行性能。CTE是ANSI SQL 99标准的一部分,在MySQL 8.0.1版本被引入。 原文地址: mytecdb.com/blogDetail.php?id=75 1. CTE优势 查询语句的可读

leetcode 刷题之路 8 Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. 寻找一组字符串的最长公共前缀,比较简单,直接贴上程序: accepted answer: class Solution {public:string longestCommonPrefix(vector<str