题意:如题,用表达式树来表示一个表达式,且消除公共的部分,即用编号表示。编号 K 定义为表达式第 K 个出现的字符串。

解法:先构造表达式树,给每棵子树用(string,left_son,right_son)-->(哈希值,...,...)编号。由于最多出现4个小写字母,所以可以用27进制数表示,同时也要利用好映射map的count(),和对应的dict[]的编号。

再递归输出表达式,利用编号的定义,看输出编号或字符串。

注意——这里的自定义比较器用的是“显式定义”,若是2个键值排序,还可以用专门把2个类型捆绑到一起的pair,它默认先比较第一关键字 .first,再是第二关键字 .second——

定义排序:typedef pair<int,int> pii[]; 再加优先队列 或 cmp函数
赋值: pii[]=make_pair(x,y);

P.S.我是参考着紫书做着Rujia Liu的代码打着,但如名,我这konjac蒟蒻改成了一个我自己能够记得的代码~所以,这题主要是提高代码能力,hh~

 1 #include<cstdio>
2 #include<cstdlib>
3 #include<cstring>
4 #include<map>
5 #include<iostream>
6 using namespace std;
7 const int N=50010;
8
9 char ss[7*N];
10 struct node
11 {
12 char s[6];
13 int hash,lc,rc,l;
14 bool operator < (const node& x) const
15 {
16 if (hash!=x.hash) return hash<x.hash;
17 if (lc!=x.lc) return lc<x.lc;
18 return rc<x.rc;
19 }
20 }a[N];
21 map<node,int> dict;
22 bool v[N];
23 int len,p;
24
25 int build()
26 {
27 int id=++len,t=p;
28 node& u=a[id];
29 u.hash=0,u.lc=u.rc=-1,u.l=0;
30 while (ss[p]>='a'&&ss[p]<='z')
31 {
32 u.hash=27*u.hash+ss[p]-'a'+1;
33 u.s[p-t+1]=ss[p];
34 p++;
35 }
36 u.l=p-t;
37 if (ss[p]=='(')
38 {
39 p++,u.lc=build();
40 p++,u.rc=build();
41 p++;
42 }
43 if (dict.count(u)) {len--;return dict[u];}
44 dict[u]=id;
45 return dict[u];
46 }
47
48 void print(int x)
49 {
50 if (v[x]) {printf("%d",x);return;}
51 v[x]=1;
52 for (int i=1;i<=a[x].l;i++) printf("%c",a[x].s[i]);
53 if (a[x].lc!=-1)
54 {
55 printf("(");
56 print(a[x].lc);
57 printf(",");
58 print(a[x].rc);
59 printf(")");
60 }
61 }
62
63 int main()
64 {
65 int T;
66 scanf("%d",&T);
67 while (T--)
68 {
69 len=0,p=1;
70 dict.clear();
71 scanf("%s",ss+1);
72 memset(v,0,sizeof(v));
73 print(build());
74 printf("\n");
75 }
76 return 0;
77 }

最新文章

  1. jquery学习笔记----元素筛选
  2. docker下部署gitlab
  3. sql语句or与union all的执行效率比较
  4. The CLR&#39;s Execution Model
  5. git 回退和删除操作
  6. linux服务器开发二(系统编程)--进程相关
  7. NowCoderWannafly挑战赛3-B.遇见
  8. Spring Mybatis多数据源配置范例
  9. 解决修改css或js文件后,浏览器缓存未更新问题
  10. 深入理解JSCore
  11. 4.93Python数据类型之(8)集合
  12. 使用idea+gradle建立SSM项目
  13. salesforce 零基础学习(六十三)Comparable实现Object列表数据的自定义排序
  14. Accounting_会计基础知识
  15. QueryableHelper
  16. 阿里大鱼短信接口(Python3版)
  17. MAC必装神器
  18. solr通过http请求搜索
  19. 【bzoj4720】【noip2016】【换座位】期望dp+Floyd
  20. sql server 性能调优 资源等待之内存瓶颈的三种等待类型

热门文章

  1. 基于JavaFX实现的音乐播放器
  2. paramunittest参数化测试基础
  3. leetcode 473. 火柴拼正方形(DFS,回溯)
  4. MybatisPlus多数据源及事务解决思路
  5. MySQL查询截取分析
  6. 入门OJ:售货员的难题
  7. 转 6 jmeter元件的作用域与执行顺序
  8. status 404 reading EduClient#getCourseInfoOrder(String)解决过程
  9. Linux下运行java报错:Error: Could not find or load main class SocketIOPropertites
  10. JAVA不可不知的强软弱虚四种引用