toj 4614 Infinite Dictionaries

时间限制(普通/Java):1000MS/3000MS 内存限制:65536KByte
总提交: 2 测试通过:2

描述

A dictionary is a set of key-value pairs, for example:

{‘color’:’red’, ‘price’:2, 7:’test’, 100:-100}

As you can see, keys and values can be strings or integers. What’s more, values can also be dictionaries or variable references. Here is the formal definition of terms that will be used soon:

key ::= INTEGER | STRING
value ::= INTEGER | STRING | dict
pair ::= key ‘:’ value
dict ::= ‘{’ [pair (‘,’ pair)*] ‘}’
var ::= ‘a’|’b’|’c’|…|’z’
slot ::= var(‘[’ key ‘]’)*
lvar ::= slot
rvar ::= slot | value

Here (‘[’ key ‘]’)* means zero or more subscripts, [pair (‘,’ pair)*] means zero or more key-value pairs.

Strings are always enclosed by single quotes (”) and consists of up to 10 lower-case letters. Integers always have absolute values of no more than 1000. You can insert spaces anywhere, except inside strings or integers. For example, { ‘a’:-1} and {‘a’ : -1 } are the same, but {‘a b’:1} and {‘a’:- 1} are both illegal.

Your task is to execute a series of commands and print the results. There are 3 kinds of commands:

  • 1. Assignment: < lvar > = < rval >

After assigning a slot to a slot (rather than a value), the left-hand slot will be holding a reference to the right-hand. For example, After executing the following commands, b[1][0] is 1, rather than 0:

a = {0:0}
b = {}
b[1] = a
a[0] = 1

Slots must be assigned before it is read or subscripted, and integers and strings cannot be subscripted. Consider the following comammd list:

c = {}
c[0] = 3
c[1] = c[0]
d[0] = ‘i’
c = d
d = c[1][‘a’]
c[2][2] = 2

The first three commands are legal, but the next two are both illegal because slot d must be assigned before it is read or subscripted. The last three are also illegal.

  • 2. Length: length(< slot >)

Output the number of key-value pairs in the slot. Note that nested pairs are not counted. For example:

a = {0: {0:0, 1:1}}
length(a)

will output 1, not 3. In this command, it is guaranteed that is storing a dictionary, not a string or an integer.

  • 3. Infinity test: test(< slot >)

If the slot can be subscripted indefinitely, output 1. Otherwise, output 0. For example, after executing the following command list:

d = {}
d[0] = d

Then d is infinite, since d[0][0][0][0][0][0]… is always d. In this command, it is guaranteed that is storing a dictionary, not a string or an integer.

输入

The input contains at most 10000 lines of commands, each line will be non-empty and will contain no more than 300 characters. All the commands are legal.

输出

Print the output (one line for each length/test command).

样例输入

c = {}
d = {‘color’: ‘red’, ‘price’: 2, 7: ‘test’, 100: -100}
length(d)
d[7] = {‘this’: ‘is’, ‘a’: ‘book’}
length(d)
d[8] = {‘this’ : ‘is’, ‘another’ : {‘a’ : ‘book’, ‘b’: ‘book2’} }
length(d)
c[7] = c
test(c)
test(d)
length(c)
d[0] = c
length(d)
test(d[0])

样例输出

4
4
5
1
0
1
6
1

提示

The term definitions in this problem use a modified BNF grammar notation that is described in the official documentation of the Python programming language. Here is the explanation of the notation, extracted from the documentation:

Each rule begins with a name (which is the name defined by the rule) and ::=. A vertical bar (|) is used to separate alternatives; it is the least binding operator in this notation. A star () means zero or more repetitions of the preceding item; likewise, a plus (+) means one or more repetitions, and a phrase enclosed in square brackets ([ ]) means zero or one occurrences (in other words, the enclosed phrase is optional). The and + operators bind as tightly as possible; parentheses are used for grouping. Literal strings are enclosed in quotes. White space is only meaningful to separate tokens. Rules are normally contained on a single line; rules with many alternatives may be formatted alternatively with each line after the first beginning with a vertical bar.

参考程序

#include<vector>
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>using namespace std;struct rec {int flag;int x;string y;int z;rec() {flag = 0;x = 0;y = "";z = 0;}
};bool operator<(rec a, rec b) {if (a.flag != b.flag)return a.flag < b.flag;if (a.x != b.x)return a.x < b.x;if (a.y != b.y)return a.y < b.y;return a.z < b.z;
}rec v[100];
map<rec, rec> a[100000];
string s;
int n;
int zhan[100000];
int used[100000];
int used1[100000];rec parsemap(int x, int y);int dfs(int x) {for (map<rec, rec>::iterator it = a[x].begin(); it != a[x].end(); it++) {if (it->second.flag == 2) {if (used[it->second.z] == 0) {used[it->second.z] = 1;used1[it->second.z] = 1;if (dfs(it->second.z)) return 1;used1[it->second.z] = 0;}else if (used1[it->second.z] == 1) return 1;}}return 0;
}int test(int x) {int top, bottom;top = 0;bottom = 1;memset(used, 0, sizeof(used));memset(used1, 0, sizeof(used1));used[x] = 1;used1[x] = 1;return dfs(x);
}rec parsekey(int x, int y) {rec p;while (s[x] == ' ') x++;while (s[y] == ' ') y--;if (s[x] == '\'') {p.flag = 1;p.x = 0;p.y = s.substr(x + 1, y - x - 1);p.z = 0;}else {p.flag = 0;p.x = atoi(s.substr(x, y - x + 1).c_str());p.y = "";p.z = 0;}return p;
}pair<rec, rec> parsepair(int x, int y) {pair<rec, rec> ans;int i;for (i = x; i <= y; i++)if (s[i] == ':') break;ans.first = parsekey(x, i - 1);i++;for (; i <= y; i++)if (s[i] != ' ') break;if (s[i] == '{') {ans.second = parsemap(i, y);}else {ans.second = parsekey(i, y);}return ans;
}rec parsemap(int x, int y) {int i, j, prev, cnt;pair<rec, rec> tmp;rec ans;ans.flag = 2;ans.x = 0;ans.y = "";ans.z = n;n++;cnt = 0;for (i = x; i <= y; i++)if (s[i] != ' ') cnt++;if (cnt == 2) {return ans;}j = 0;i = x + 1;prev = x;while (1) {for (; i < y; i++) {if (s[i] == '{') j++;if (s[i] == '}') j--;if ((j == 0) && (s[i] == ',')) break;}tmp = parsepair(prev + 1, i - 1);a[ans.z][tmp.first] = tmp.second;if (i == y) {break;}prev = i;i++;}return ans;
}pair<int, rec> parseleft(int x, int y) {pair<int, rec> p;rec q;int i, l, r;for (i = x; i <= y; i++) if (s[i] != ' ') break;p.first = s[i] - 'a';p.second.flag = -1;p.second.x = 0;p.second.y = "";p.second.z = 0;while (1) {i++;for (; i <= y && i < s.length(); i++)if (s[i] == '[') break;l = i;for (; i <= y && i < s.length(); i++)if (s[i] == ']') break;r = i;if (i > y) break;q = parsekey(l + 1, r - 1);if (p.second.flag == -1) {p.first = v[p.first].z;p.second = q;}else {p.first = a[p.first][p.second].z;p.second = q;}}return p;
}rec parseright(int x, int y) {rec ans;while (s[x] == ' ') x++;while (s[y] == ' ') y--;if ((s[x] >= 'a') && (s[x] <= 'z')) {pair<int, rec> tmp = parseleft(x, y);if (tmp.second.flag == -1) {ans.flag = 2;ans.x = 0;ans.y = "";ans.z = v[tmp.first].z;}else {ans = a[tmp.first][tmp.second];}return ans;}if (s[x] == '{') {ans = parsemap(x, y);return ans;}ans = parsekey(x, y);return ans;
}int main() {int i, p;char ss[300];n = 0;for (i = 0; i < 26; i++)v[i].z = -1;while (1) {ss[0] = 0;gets(ss);if (ss[0] == 0) break;s = ss;p = s.find_first_of('=');if (p == -1) {p = s.find_first_of('(');pair<int, rec> tmp = parseleft(p + 1, s.length() - 2);int ind;if (tmp.second.flag == -1)ind = v[tmp.first].z;elseind = a[tmp.first][tmp.second].z;if (s[0] == 'l') {printf("%d\n", a[ind].size());}else {printf("%d\n", test(ind));}}else {p = s.find_first_of('=');pair<int, rec> tmp = parseleft(0, p - 1);if (tmp.second.flag == -1)v[tmp.first] = parseright(p + 1, s.length() - 1);elsea[tmp.first][tmp.second] = parseright(p + 1, s.length() - 1);}}return 0;
}

toj 4614 Infinite Dictionaries相关推荐

  1. ADPRL - 近似动态规划和强化学习 - Note 3 - Stochastic Infinite Horizon Problem

    Stochastic Infinite Horizon Problem 3.Stochastic Infinite Horizon Problem 定义3.1 无限范围的马尔可夫决策过程 (Marko ...

  2. pandas基于元组列表(list of tuples)、列表词典(dictionary of lists)、词典列表(list of dictionaries)构建dataframe数据实战

    pandas基于元组列表(list of tuples).列表词典(dictionary of lists).词典列表(list of dictionaries)构建dataframe数据实战 目录

  3. dosbox edit.exe 如何使用_如何实现摄影后期无限色彩调色?Infinite Color

    如何实现摄影后期无限色彩调色? 无限配色调色插件 Infinite Color Panel 紫枫今天发布的是一款神奇的后期插件,可以实现无限色彩配色调色,作为一个摄影人,在拍摄完成后如何的调色配色,色 ...

  4. TOJ 1702.A Knight's Journey

    2015-06-05 问题简述: 有一个 p*q 的棋盘,一个骑士(就是中国象棋里的马)想要走完所有的格子,棋盘横向是 A...Z(其中A开始 p 个),纵向是 1...q. 原题链接:http:// ...

  5. lists and Variables supported as JIT inputs/outputs. Dictionaries and strings are also accepted but

    Only tuples, lists and Variables supported as JIT inputs/outputs. Dictionaries and strings are also ...

  6. Python编程基础:第十八节 字典Dictionaries

    第十八节 字典Dictionaries 前言 实践 前言 字典的定义方式与集合相似,也是通过花括号{}进行定义的,不同的是字典中的每一个元素由两部分构成,分别是键和值.字典中的元素都是无序的,并且元素 ...

  7. hdu 4614 Vases and Flowers

    http://acm.hdu.edu.cn/showproblem.php?pid=4614 题意:有N个花瓶,标号为0-N-1,往每一个花瓶放一朵花,然后有M个操作,输入a,b,c,如果a==1表示 ...

  8. Could not write JSON: Infinite recursion (StackOverflowError);

    转自:https://blog.csdn.net/east123321/article/details/80435051 在controller返回数据到统一json转换的时候,出现了json inf ...

  9. uva 10627 - Infinite Race(数论)

    题目链接:uva 10627 - Infinite Race 题目大意:一段路.两个人在这条路上来回走,求相遇次数 解题思路:相遇有两种,一种是追击,一种是相对 追击:t∗(u−v)=(2∗k+1)∗ ...

最新文章

  1. 创建c语言编译错误,创建C语言项目时,无法编译成*.exe文件,提示系统找不到指定的文件...
  2. 关于客户端用ASP参生报表
  3. 执行java脚本_怎么用bat执行java应用程序
  4. WinForm登录模块设计开发
  5. [转载] java synchronized静态同步方法与非静态同步方法,同步语句块
  6. python重定向网页_【Python网页分析】httplib库的重定向处理
  7. 前端之JQuery:JQuery属性操作
  8. 移动金库模式保护客户信息 覆盖17套关键系统
  9. PTA 程序设计天梯赛(1~180题)
  10. samurai_ii__vengeance(武士二:复仇)无法在android上运行的原因
  11. android lomo设计与实现,拍静物 美图秀秀Android轻松调LOMO风格
  12. pomodoro源码
  13. 传统企业如何应对数字化转型?这篇文章给你答案
  14. 2018最新苹果APP上架App Store流程(超详细)
  15. 寻找AR中的Big Difference - v2.0 | MixLab AR指南
  16. 圆锥形怎么画_草图大师怎么画圆锥形?
  17. Word加粗的字体如何恢复正常粗细
  18. 2017第二届广东省强网杯线上赛——WEB-who are you?
  19. 网上订餐系统设计与实现(JSP+SSM+MySQL)
  20. ORB - (Oriented Fast and Rotated BRIEF)算法

热门文章

  1. 仿微信选取图片发表朋友圈功能
  2. 【概念集锦】之 shim和polyfill
  3. js 请求接口获取不到登录cookie xhrFields 配置
  4. C#开发笔记,点点细微,处处真情,记录开发中的痛点
  5. C#LeetCode刷题之#326-3的幂(Power of Three)
  6. SAS在金融中的应用六
  7. 122_Power PivotPower BI不连续日期的日环比
  8. 这5个有趣的Python库带你花式编码!
  9. Flask 正则路由匹配——转换器
  10. Python treelib库创建多叉树的用法介绍