Gym 100513M - Variable Shadowing
来源:互联网 发布:淘宝被扣24分还能用吗 编辑:程序博客网 时间:2024/06/16 09:27
In computer programming, variable shadowing occurs when a variable declared within a certain scope has the same nameas a variable declared in an outer scope. The outer variable is said to beshadowed by the inner variable, and this can lead to a confusion. If multipleouter scopes contain variables with the same name, the variable in the nearestscope will be shadowed.
Formally, a declared variable shadows another declared variable if thefollowing conditions are met simultaneously:
· the othervariable is declared in outer scope and before (in terms of position in programsource code) the declaration of the first variable,
· the othervariable is nearest among all variables satisfying the condition above.
Here is an example containing exactly one variable shadowing:
/* Prints a+max(b,c) */
int main() {
int a, b, c;
cin » a » b » c;
if (b > c) {
int a = b; // <– variable 'a'shadows outer 'a'
int x = c;
b = x;
c = a;
}
int x = a + c; // <– no shadowinghere
cout « x « endl;
}
Variable shadowing is permitted in many modern programming languagesincluding C++, but compilers can warn a programmer about variable shadowing toavoid possible mistakes in a code.
Consider a trivial programming language that consists only of scopes andvariable declarations. The program consists of lines, each line contains onlycharacters '{', '}', 'a' ... 'z' separated by one or more spaces.
· Scopes. A scope (excluding global) is bounded with a pair of matching curlybrackets '{' and '}'. A scope is an inner scope relative to another scope if brackets ofthe first scope are enclosed by brackets of the second scope.
· Variables. A variable declaration in this language is written just as a name of thevariable. In addition all variables are lowercase Latin letters from 'a' to 'z'inclusive (so there are at most 26 variable names). A variable is declared ineach scope at most once.
Given a syntactically correct program (i.e. curly brackets form a regular bracket sequence), writean analyzer to warn about each fact of variable shadowing. Warnings shouldinclude exact positions of shadowing and shadowed variables. Your output shouldfollow the format shown in the examples below.
Input
The first line contains integer n (1 ≤ n ≤ 50)— the number of lines in theprogram. The following n lines contain the program. Each program line consists of tokens '{', '}', 'a' ... 'z'separated by one or more spaces. The length of each line is between 1 and 50characters. Each program line contains at least one non-space character.
The curly brackets in the program form a regularbracket sequence, so each opening bracket '{' has uniquely defined matching closing bracket '}' and vice versa. A variable is declared in a scope at most once. Anyscope (including global) can be empty, i.e. can contain no variabledeclarations.
Output
For each fact of shadowing write a line in form "r1:c1:warning: shadowed declaration of ?, the shadowed position is r2:c2", where "r1:c1" is the number of line and position in line of shadowingdeclaration and "r2:c2" is the number of line and position in line of shadoweddeclaration. Replace '?' with the letter 'a' ... 'z' — the nameof shadowing/shadowed variable. If multiple outer scopes have variables namedas the shadowing variable, the variable in the nearest outer scope is shadowed.
Print warnings in increasing order of r1, or in increasing order of c1 if values r1 are equal. Leave the output empty if there are no variable shadowings.
Sample test(s)
input
1
{ a { b { a } } } b
output
1:11:warning: shadowed declaration of a, the shadowed position is 1:3
input
1
{ a { a { a } } }
output
1:7: warning:shadowed declaration of a, the shadowed position is 1:3
1:11: warning: shadowed declaration of a, the shadowed position is 1:7
input
7
a b {
a { a }
c b {
c
} }
{ a d z
} z
output
2:2: warning:shadowed declaration of a, the shadowed position is 1:1
2:6: warning: shadowed declaration of a, the shadowed position is 2:2
3:6: warning: shadowed declaration of b, the shadowed position is 1:3
4:3: warning: shadowed declaration of c, the shadowed position is 3:2
6:3: warning: shadowed declaration of a, the shadowed position is 1:1
input
1
{ a } { b } a { a } { a } { a x { a b x } } b x
output
1:17:warning: shadowed declaration of a, the shadowed position is 1:13
1:23: warning: shadowed declaration of a, the shadowed position is 1:13
1:29: warning: shadowed declaration of a, the shadowed position is 1:13
1:35: warning: shadowed declaration of a, the shadowed position is 1:29
1:39: warning: shadowed declaration of x, the shadowed position is 1:31
思路:
模拟,用栈来判断如今在第几层括号,另外开数组记录当前有哪些变量及个数,注意以下细节就能过
程序:#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <cstring>#include <cstdio>#include <stack>using namespace std;const int N = 100005;struct node{int x, y;};int step[N];char var[N];int main(){stack<node> q[200];memset(step, 0, sizeof(step));node temp;int n;scanf("%d\n", &n);int i, num = 0, v = 0;char st[N];for (i = 1; i <= n; i++){cin.getline(st, N, '\n');int len = strlen(st);int j;for (j = 0; j<len; j++){if (st[j] == ' ')continue;if (st[j] == '{'){num++;}elseif (st[j] == '}'){for (; step[num]>0; step[num]--){q[var[v - 1]].pop();v--;}num--;// cout<<step[num]<<endl;}else{if (!q[st[j]].empty()){printf("%d:%d: warning: shadowed declaration of %c, the shadowed position is %d:%d\n", i, j + 1, st[j], q[st[j]].top().x, q[st[j]].top().y);}temp.x = i;temp.y = j + 1;q[st[j]].push(temp);var[v++] = st[j];step[num]++;}}}return 0;}
- Gym 100513M - Variable Shadowing
- Problem M. Variable Shadowing -字符串暴力处理
- codeforces Variable Shadowing 2014 NEERC Southern Subregional M 贪心策略
- codeforces Variable Shadowing 栈的模拟
- Gym 100187M - Heaviside Function
- Gym 100703M It's complicated 水题
- Gym 101149M Ex Machina[线段树]
- GYM 101149 M.Ex Machina(构造)
- override new shadowing
- Codeforces Gym 100971M Decomposition into Good Strings DP+数据结构
- GYM 100090 M.Jumping along the Hummocks(SPFA)
- GYM 100488 M.Construct a Permutation(构造)
- GYM 100694 M.The Fifth Season(博弈论)
- Gym 101341M Last Man Standing 模拟|贪心
- Gym 101086 M ACPC Headquarters : AASTMT (Stairway to Heaven)
- [gym 101149 M. Ex Machina]构造+线段树
- Codeforces Gym 100513
- Gym
- 妈妈再也不担需要编译的python包了
- 重载操作符
- web前端学习记录(一)
- iOS开发之软键盘使用小技巧
- Speedup,Scaleup,Sizeup
- Gym 100513M - Variable Shadowing
- Java多线程实现生产者消费者模式
- struts.xml的结构
- HDU 3501 Calculation 2(欧拉函数)
- 杭电1873 看病要排队
- 结构体运用
- hdu 4444 Walk(预处理+暴力)
- HDOJ 5323 Solve this interesting problem BFS搜索
- I - Pots -BFS