博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Gym 100513M M. Variable Shadowing 暴力
阅读量:5038 次
发布时间:2019-06-12

本文共 5134 字,大约阅读时间需要 17 分钟。

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

 

题意

给你一堆字符串,让你找到变量的父亲是啥

题解:

暴力找就好了……

代码

 

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
typedef long long ll;using namespace std;//freopen("D.in","r",stdin);//freopen("D.out","w",stdout);#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)#define test freopen("test.txt","r",stdin)const int maxn=202501;#define mod 1000000007#define eps 1e-9const int inf=0x3f3f3f3f;const ll infll = 0x3f3f3f3f3f3f3f3fLL;inline ll 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;}//**************************************************************************************struct node{ int x,y;};node a[maxn];char ss[101][101];string s;int main(){ int n=read(); for(int i=0;i
='a') { int flag=0; int ans=0; for(int j=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--; } } }}

 

 

 

 

转载于:https://www.cnblogs.com/qscqesze/p/4671387.html

你可能感兴趣的文章
创建与删除索引
查看>>
java的基本数据类型
查看>>
机器学些技法(9)--Decision Tree
查看>>
静态页面复习--用semantic UI写一个10min首页
查看>>
在Windows下安装64位压缩包版mysql 5.7.11版本的方法
查看>>
drf权限组件
查看>>
输入月份和日期,得出是今年第几天
查看>>
利用mysqldump备份mysql
查看>>
Qt中子窗口全屏显示与退出全屏
查看>>
使用brew安装软件
查看>>
[BZOJ1083] [SCOI2005] 繁忙的都市 (kruskal)
查看>>
吴裕雄 python 机器学习——数据预处理嵌入式特征选择
查看>>
Centos6.4安装JDK
查看>>
201521123069 《Java程序设计》 第4周学习总结
查看>>
线性表的顺序存储——线性表的本质和操作
查看>>
【linux】重置fedora root密码
查看>>
用swing做一个简单的正则验证工具
查看>>
百度坐标(BD-09)、国测局坐标(火星坐标,GCJ-02)和WGS-84坐标互转
查看>>
pig自定义UDF
查看>>
输入名字显示其生日,没有则让输入生日,做记录
查看>>