Time Limit:20 Sec
Memory limit:256 MB
Topic Connection
Http://codeforces.com/gym/100513/problem/M
Description
In computer programming, variable shadowing occurs when a variable declared within a certain scope have the same name a s a variable declared in an outer scope. The outer variable is said to being shadowed by the inner variable, and this can leads to a confusion. If multiple outer scopes contain variables with the same name, the variable in the nearest scope would be shadowed.
Formally, a declared variable shadows another declared variable if the following conditions are met simultaneously:
- The other variable are declared in outer scopes and before (in terms of position on program source code) the Declaration of The first variable,
- The other variable are nearest among all variables satisfying the condition above.
Here are 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 shadowing here
Cout«x«endl;
}
Variable shadowing is permitted in many modern programming languages including C + +, but compilers can warn a programmer ab Out variable shadowing to avoid possible mistakes in a code.
Consider a trivial programming language that consists only of scopes and variable declarations. The program consists of lines, each line contains only characters ' {', ' a" ... ' z ' separated by one or more spaces.
- scopes. a scope (excluding global) is bounded with A pair of matching curly Brackets ' {' and ' } '. A scope is an inner scope relative to another scope if brackets of the first scope be enclosed by brackets of the second Scope.
- variables. a variable declaration in this language is written just as A name of T He variable. In addition all variables is lowercase Latin letters from ' a ' to '
Given a syntactically correct program (i.e. curly brackets form a regular bracket sequence), write the analyzer to warn About each fact of variable shadowing. Warnings should include exact positions of shadowing and shadowed variables. Your output should follow the format shown in the examples below.
Input
The first line contains Integer n (1≤ N ≤50)-the number of lines in the program. 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 characters. Contains at least one non-space character.
The curly brackets in the program form a regular bracket sequence, so all opening bracket '{' has uniquely define D Matching closing bracket '} ' and vice versa. A variable is declared in a scope at the most once. Any scope (including global) can is empty, i.e. can contain no variable declarations.
Output
For each fact of shadowing write a line in form "r1:c1:warning:shadowed declaration o F?, the shadowed position is R2:c2 ", where" R1:C1 "are the number of line and position in Line of shadowing declaration and "R2:C2" are the number of line and position in line of SH adowed declaration. Replace ' ' a ' ... ' Z ' -the name of shadowing/shadowed variable. If multiple outer scopes has variables named as the shadowing variable, the variable in the nearest outer scope is shadow ed.
Print warnings in increasing order of R1, or in increasing order of C1 if values r1 is equal. Leave the output empty if there is no variable shadowings.
Sample Input
1
{a {b {a}}} b
Sample Output
1:11:warning:shadowed Declaration of A, the shadowed position is 1:3
HINT
Test instructions
Give you a bunch of strings that let you find the variable what the father is
Exercises
Violence to find just fine ...
Code
#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>#include<stack>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineTest Freopen ("Test.txt", "R", stdin)Const intmaxn=202501;#defineMoD 1000000007#defineEPS 1e-9Const intinf=0x3f3f3f3f;Constll infll =0x3f3f3f3f3f3f3f3fll;inline ll Read () {ll x=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}//**************************************************************************************structnode{intx, y;}; Node A[MAXN];Charss[101][101];strings;intMain () {intn=read (); for(intI=0; i<n;i++) gets (Ss[i]); intnum=0; for(intI=0; i<n;i++) for(intj=0; J<strlen (Ss[i]); J + +) {s+=Ss[i][j]; a[num].x=i,a[num++].y=J; } for(intI=0; I<s.size (); i++) { if(s[i]<='Z'&&s[i]>='a') { intflag=0; intans=0; for(intj=i-1; j>=0; j--) { if(ans==0&&s[j]==S[i]) {printf ("%d:%d:warning:shadowed Declaration of%c, the shadowed position is%d:%d\n", a[i].x+1, a[i].y+1, s[i],a[j].x+1, a[j].y+1); Flag=1; } if(flag) Break; if(s[j]=='}') ans++; if(s[j]=='{'&&ans>0) ans--; } } }}
Codeforces Gym 100513M M. Variable shadowing violence