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
题意
给你一堆字符串,让你找到变量的父亲是啥
题解:
暴力找就好了……
代码
#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;
//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=;
#define mod 1000000007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** struct node
{
int x,y;
};
node a[maxn];
char ss[][];
string s;
int main()
{
int n=read();
for(int i=;i<n;i++)
gets(ss[i]);
int num=;
for(int i=;i<n;i++)
for(int j=;j<strlen(ss[i]);j++)
{
s+=ss[i][j];
a[num].x=i,a[num++].y=j;
}
for(int i=;i<s.size();i++)
{
if(s[i]<='z'&&s[i]>='a')
{
int flag=;
int ans=;
for(int j=i-;j>=;j--)
{
if(ans==&&s[j]==s[i])
{
printf("%d:%d: warning: shadowed declaration of %c, the shadowed position is %d:%d\n",a[i].x+,a[i].y+,s[i],a[j].x+,a[j].y+);
flag=;
}
if(flag)
break;
if(s[j]=='}')
ans++;
if(s[j]=='{'&&ans>)
ans--;
} }
}
}
Codeforces Gym 100513M M. Variable Shadowing 暴力的更多相关文章
- Codeforces Gym 100513G G. FacePalm Accounting 暴力
		
G. FacePalm Accounting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513 ...
 - Codeforces Gym 100002  C "Cricket Field" 暴力
		
"Cricket Field" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1000 ...
 - Codeforces Gym 100342E Problem E. Minima 暴力
		
Problem E. MinimaTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/attac ...
 - Codeforces Gym 100203G G - Good elements 暴力
		
G - Good elementsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...
 - Codeforces Gym 101190M Mole Tunnels - 费用流
		
题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...
 - Codeforces Gym 101252D&&floyd判圈算法学习笔记
		
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
 - Codeforces Gym 101623A - 动态规划
		
题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...
 - 【Codeforces Gym 100725K】Key Insertion
		
Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...
 - Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】
		
2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...
 
随机推荐
- 【Java集合框架】规则集--Set
			
集合: Java主要支持三种: 1.规则集(Set) 用于存储一组不重复的元素 2.线性表(List) 用于存储一个由元素构成的有序集合 3.队列(Queue) 同与数据结构中的队列,存储用先进先出的 ...
 - 【转】Android Studio简单设置
			
原文网址:http://ask.android-studio.org/?/article/14 Android Studio 简单设置 界面设置 默认的 Android Studio 为灰色界面,可以 ...
 - UITextField限制字数的方法
			
转:http://blog.csdn.net/marujunyy/article/details/9985411 在输入东西的时候,如果想限制最大字数,可以用下面方法: - (BOOL)textFie ...
 - 编译boost (windows msvc14)
			
我的环境 OS: WIN10 (x64) IDE: VS2015 (VC14) http://www.boost.org/ 1. 下载 下载boost包, boost_1_62_0.7z 使用ASIO ...
 - POJ 2362  Square
			
题意:给n个木棍,问能不能正好拼成一个正方形. 解法:POJ1011的简单版……不需要太多剪枝……随便剪一剪就好了……但是各种写屎来着QAQ 代码: #include<stdio.h> # ...
 - hdu 3038 How Many Answers Are Wrong(种类并查集)2009 Multi-University Training Contest 13
			
了解了种类并查集,同时还知道了一个小技巧,这道题就比较容易了. 其实这是我碰到的第一道种类并查集,实在不会,只好看着别人的代码写.最后半懂不懂的写完了.然后又和别人的代码进行比较,还是不懂,但还是交了 ...
 - IDEA与Tomcat创建并运行Java Web项目及servlet的简单实现
			
创建Web项目 1. File -> New Project ,进入创建项目窗口 2.在 WEB-INF 目录下点击右键, New -> Directory ,创建 classes 和 ...
 - HDU 4911          Inversion
			
http://acm.hdu.edu.cn/showproblem.php?pid=4911 归并排序求逆对数. Inversion Time Limit: 2000/1000 MS (Java/ ...
 - cocos2d anchor point 锚点解析
			
anchor point 究竟是怎么回事? 之所以造成不容易理解的是因为我们平时看待一个图片是 以图片的中心点 这一个维度来决定图片的位置的.而在cocos2d中决定一个 图片的位置是由两个维度 一个 ...
 - bzoj 2132 圈地计划(黑白染色,最小割)
			
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2132 [题意] 给定n*m个区域,建工业区价值A,建商业区价值B,如果(i,j)有k个 ...