Codeforces Gym 100513M M. Variable Shadowing 暴力

M. Variable Shadowing

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/gym/100513/problem/M

Description

In computer programming, variable shadowing occurs when a variable declared within a certain scope has the same name as a variable declared in an outer scope. The outer variable is said to be shadowed by the inner variable, and this can lead to a confusion. If multiple outer scopes contain variables with the same name, the variable in the nearest scope will be shadowed.

Formally, a declared variable shadows another declared variable if the following conditions are met simultaneously:

  • the other variable is declared in outer scope and before (in terms of position in program source code) the declaration of the first variable,
  • the other variable 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 shadowing here    cout « x « endl;}

Variable shadowing is permitted in many modern programming languages including C++, but compilers can warn a programmer about 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 are enclosed by brackets of the second scope.
  • Variables. A variable declaration in this language is written just as a name of the variable. 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 in each scope at most once.

Given a syntactically correct program (i.e. curly brackets form a regular bracket sequence), write an 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 50 characters. Each program line contains at least one non-space character.

The curly brackets in the program form a regular bracket sequence, so each opening bracket ‘{‘ has uniquely defined matching closing bracket ‘}‘ and vice versa. A variable is declared in a scope at most once. Any scope (including global) can be empty, i.e. can contain no variable declarations.

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 shadowing declaration and "r2:c2" is the number of line and position in line of shadowed declaration. Replace ‘?‘ with the letter ‘a‘ ... ‘z‘ — the name of shadowing/shadowed variable. If multiple outer scopes have variables named as 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 Input

1
{ a { b { a } } } b

Sample Output

1:11: warning: shadowed declaration of a, the shadowed position is 1:3

HINT

题意

给一段代码,要求找到每一处外部变量被内部变量覆盖的地方

题解:

乍一看有点难,甚至想到堆栈什么的,但是由于规模很小,只要借助{、},开一个flag变量,然后暴力往回扫描找到被覆盖的变量即可。

//debug:在把s改成tmp的过程中忘记吧s.length改成tmp.length

代码

#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>
typedef long long ll;
using namespace std;
#define test freopen("1.txt","r",stdin)
#define maxn 2000001
#define mod 10007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline int read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
inline void out(int x) {
   if(x>9) out(x/10);
   putchar(x%10+‘0‘);
}
//**************************************************************************************
int numr[3000];
int numc[3000];
int main()
{
    int n=read();
    string s;
    for(int i=0,p=0;i<n;i++){
        string tmp;
        getline(cin,tmp);
        s+=tmp;
        for(int j=0;j<tmp.length();j++){
            numr[p]=i;
            numc[p++]=j;
        }
    }
    for(int i=0;i<s.length();i++){
            if(s[i]>=‘a‘&&s[i]<=‘z‘){
                int flag=0,j=i-1;
                for(;j>=0;j--){
                    if(s[j]==‘}‘) flag--;
                    else if(s[j]==‘{‘&&flag!=1) flag++;
                    if(flag==1&&s[i]==s[j]){
                        //printf("%d %c %c\n",flag,s[i],s[j]);
                        printf("%d:%d: warning: shadowed declaration of %c, the shadowed position is %d:%d\n",numr[i]+1,numc[i]+1,s[i],numr[j]+1,numc[j]+1);
                        break;
                    }
                }
            }
    }
    return 0;
}
时间: 2024-10-06 02:55:29

Codeforces Gym 100513M M. Variable Shadowing 暴力的相关文章

Codeforces Gym 100002 C &quot;Cricket Field&quot; 暴力

"Cricket Field" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100002 Description Once upon a time there was a greedy King who ordered his chief Architect to build a field for royal cricket inside his park. The King was so

Codeforces Gym 100513G G. FacePalm Accounting 暴力

G. FacePalm Accounting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/problem/G Description An owner of a small company FacePalm has recently learned that the city authorities plan to offer to small businesses to partic

codeforces Gym 100500C C. ICPC Giveaways 暴力

Problem C. ICPC GiveawaysTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/attachments Description During the preparation for the ICPC contest, the organizers prepare bags full of giveaways for the contestants. Each bag us

Codeforces gym Hello 2015 Div1 B and Div2 D

Codeforces gym 100571 problem D Problem 给一个有向图G<V,E>和源点S,边的属性有长度L和颜色C,即E=<L,C>.进行Q次询问,每次给定一个点X,输出S到X的最短路的长度(不存在则输出 -1).但要求S到X的路径中相邻两条边颜色不一样. Limits Time Limit(ms): 1000 Memory Limit(MB): 256 |V|, |E|: [1, 10^5] X, S: [1, |V| ] L: [1, 10^9] |C|

Codeforces 442B Kolya and Tandem Repeat(暴力)

题目连接:Codeforces 442B Kolya and Tandem Repeat 题目大意:给出一个字符串,可以再添加n个字符,问说可以找到SS的子串形式,S尽量长. 解题思路:枚举长度和起点判断即可,超过len的可以作为任意值,但是超过len+n就不行了. #include <cstdio> #include <cstring> const int N = 205; int n, len; char s[N]; bool judge (int l) { if (l <

Codeforces gym Hello 2015 Div1 E

Codeforces gym 100570 problem E (一种处理动态最长回文子串问题的方法) Problem 给一个长度为N的字符串S,字符集是'a'-'z'.进行Q次操作,操作分三种.一,修改位置X的字符为C:二,查询以P位置为中心的最长回文子串的长度,并输出:三,查询以P与P+1的中间位置为中心的最长回文子串的长度,并输出. More 第二种操作子串长度为奇数,一定存在:第三种操作子串长度为偶数,若不存在,输出 -1. Limits Time Limit(ms): 4000(1s足

Codeforces Round #224 (Div. 2) D 暴力搜索加记忆化

题意读了半年,英语太渣,题意是摆两个棋子在棋盘上作为起点,但是起点不能在#上,然后按照图的指示开始走, < 左 > 右 ^上 v下,走的时候只能按照图的指示走,如果前方是 #的话,可以走进去,但是 走进去之后便不能再走了,走的途中两个棋子不能相碰,但是最终都走到同一个#里是没事的,并且若是能走 无限步的话 输出 -1, 例如  > < 这样左右左右的走就能无限走,然后问你 两个棋子走的最大步数的和 一开始被输出-1给困住了,因为除了 .> <这样以外  还可以刚好形成一

Codeforces gym Hello 2015 Div1 C and Div2 E

Codeforces gym 100570 problem C Codeforces gym 100571 problem E Problem 给一个N行M列的矩阵Ma,进行Q次(Q<=10)查询,每次给定一个K,问有多少子矩阵,满足最大值max - 最小值min <=K. Limits Time Limit(ms): 8000 Memory Limit(MB): 512 N, M: [1, 400] Q: [1, 10] Ma(i, j), K: [1, 10^9] Solution (Th

【模拟】ECNA 2015 I What&#39;s on the Grille? (Codeforces GYM 100825)

题目链接: http://codeforces.com/gym/100825 题目大意: 栅栏密码.给定N(N<=10),密钥为一个N*N的矩阵,'.'代表空格可以看到,'X'代表被遮挡,还有密文字符串S,长度为N*N 每次将这个矩阵顺时针旋转90°,把矩阵中空格对应的位置按照从上到下从左到右的顺序依次填充上密文字符,求最终这个密文字符能否填满N*N的矩阵,能按顺序输出得到的答案,不能输出"invalid grille" 题目思路: [模拟] 直接模拟即可.旋转的坐标公式很好推.