A1330 | [COCI-2015_2016-olympiad]#1 Dijamant
来源COCI
时间限制1s
内存限制128MB
通过 / 提交0/0
题目描述
We are observing class declarations in an object-oriented programming language similar to C++. Each class declaration is of the form “K : P1 P2 . . . Pk ;” where K is the name of the new class being declared, and P1, P2, . . . , Pk the names of classes being inherited by class K. For example, “shape :
;” is a declaration of class “shape” that does not inherit any other class, whereas “square : shape rectangle ;” is a declaration of class “square” that inherits classes “shape” and “rectangle”.
If class K1 inherits class K2, class K2 inherits class K3, and so on, up to class Km−1 that inherits class Km, then we say that all classes K1, K2, . . . , Km−1 are derived from class Km. The rules of the programming language forbid circular definitions, so it is not allowed to have a class derived from itself. In other words, the class hierarchy forms a directed acyclic graph. Additionally, it is not allowed for a so-called diamond to appear in the class hierarchy. A diamond consists of four different classes A, B, X, Y such that it holds:
• Classes X and Y are derived from A.
• Class B is derived from both X and Y .
• Neither is class X derived from Y , nor is class Y derived from X.
You are given a series of class declarations to be processed sequentially, and determine for each one whether it is correctly declared. The correctly declared classes are added to the hierarchy, while the incorrect ones are discarded. Declaration “K : P1 P2 . . . Pk ;” is correctly declared if the following holds:
1. Class K hasn’t been declared yet.
2. All classes P1, P2, . . . , Pk have been previously declared. Notice that this condition ensures that a class can never be derived from itself, or that cycles cannot exist in the class hierarchy.
3. By adding class K that inherits P1, P2, . . . , Pk the class hierarchy remains in order, that is, not a single diamond is formed.
Write a programme that will process the declarations respectively as described above and determine the correctness of each one of them.
;” is a declaration of class “shape” that does not inherit any other class, whereas “square : shape rectangle ;” is a declaration of class “square” that inherits classes “shape” and “rectangle”.
If class K1 inherits class K2, class K2 inherits class K3, and so on, up to class Km−1 that inherits class Km, then we say that all classes K1, K2, . . . , Km−1 are derived from class Km. The rules of the programming language forbid circular definitions, so it is not allowed to have a class derived from itself. In other words, the class hierarchy forms a directed acyclic graph. Additionally, it is not allowed for a so-called diamond to appear in the class hierarchy. A diamond consists of four different classes A, B, X, Y such that it holds:
• Classes X and Y are derived from A.
• Class B is derived from both X and Y .
• Neither is class X derived from Y , nor is class Y derived from X.
You are given a series of class declarations to be processed sequentially, and determine for each one whether it is correctly declared. The correctly declared classes are added to the hierarchy, while the incorrect ones are discarded. Declaration “K : P1 P2 . . . Pk ;” is correctly declared if the following holds:
1. Class K hasn’t been declared yet.
2. All classes P1, P2, . . . , Pk have been previously declared. Notice that this condition ensures that a class can never be derived from itself, or that cycles cannot exist in the class hierarchy.
3. By adding class K that inherits P1, P2, . . . , Pk the class hierarchy remains in order, that is, not a single diamond is formed.
Write a programme that will process the declarations respectively as described above and determine the correctness of each one of them.
输入格式
The first line of input contains the integer n – the number of declarations. Each of the following n lines contains a single declaration in the form of “K : P1 P2 . . . Pk ;” where P1, P2, . . . , Pk is a series of zero,
one or more classes that class K inherits. All class names in a single declaration K, P1, P2, . . . , Pk are mutually different. Each class name is a string of at most 10 lower case letters of the English alphabet.
All the elements of a declaration (the class names and characters “:” and “;”) are separated by exactly one space. In each specific declaration, for the number of classes k it holds 0 ≤ k ≤ 1 00
0.
one or more classes that class K inherits. All class names in a single declaration K, P1, P2, . . . , Pk are mutually different. Each class name is a string of at most 10 lower case letters of the English alphabet.
All the elements of a declaration (the class names and characters “:” and “;”) are separated by exactly one space. In each specific declaration, for the number of classes k it holds 0 ≤ k ≤ 1 00
0.
输出格式
You must output n lines. The i th line must contain “ok” if the i th declaration is correct, and “greska” if it isn’t.
输入输出样例
输入 #1
10 shape : ; rectangle : shape ; circle : shape ; circle : ; square : shape rectangle ; runnable : object ; object : ; runnable : object shape ; thread : runnable ; applet : square thread ;
输出 #1
ok ok ok greska ok greska ok ok ok greska
输入 #2
9 a : ; x : ; b : a x ; c : b ; d : a b c ; e : d a ; f : c e ; y : x ; g : c y e ;
输出 #2
ok ok ok ok ok ok ok ok greska
Clarification of the first example:
• The fourth declaration is incorrect because class “circle” has already been defined in the third row.
• The sixth declaration is incorrect because class “object” hasn’t been defined yet.
• The eighth declaration is correct because class “object” has now been declared, and the sixth
declaration was discarded, so class “runnable” hasn’t been defined yet.
• The tenth declaration is incorrect because otherwise the following diamond forms: “shape”, “applet”,
“square”, “runnable”.
Clarification of the second example:
• The tenth declaration is incorrect because otherwise the following diamond forms: “x”, “g”, “y”, “d”
(and many other).
• The fourth declaration is incorrect because class “circle” has already been defined in the third row.
• The sixth declaration is incorrect because class “object” hasn’t been defined yet.
• The eighth declaration is correct because class “object” has now been declared, and the sixth
declaration was discarded, so class “runnable” hasn’t been defined yet.
• The tenth declaration is incorrect because otherwise the following diamond forms: “shape”, “applet”,
“square”, “runnable”.
Clarification of the second example:
• The tenth declaration is incorrect because otherwise the following diamond forms: “x”, “g”, “y”, “d”
(and many other).
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted