zoukankan      html  css  js  c++  java
  • 【技巧性(+递归运用)】UVa 1596

    In this problem, we consider a simple programming language that has only declarations of onedimensional integer arrays and assignment statements. The problem is to find a bug in the given program.

    The syntax of this language is given in BNF as follows:

    $ langle$program$ 
angle$ ::= $ langle$declaration$ 
angle$|$ langle$program$ 
angle$$ langle$declaration$ 
angle$|$ langle$program$ 
angle$$ langle$assignment$ 
angle$
    $ langle$declaration$ 
angle$ ::= $ langle$array$ \_name$$ 
angle$ [ $ langle$number$ 
angle$ ] $ langle$new$ \_line$$ 
angle$
    $ langle$assignment$ 
angle$ ::= $ langle$array$ \_name$$ 
angle$ [ $ langle$expression$ 
angle$ ]= $ langle$expression$ 
angle$$ langle$newline$ 
angle$
    $ langle$expression$ 
angle$ ::= $ langle$number$ 
angle$|$ langle$array$ \_name$$ 
angle$ [ $ langle$expression$ 
angle$ ]
    $ langle$number$ 
angle$ ::= $ langle$digit$ 
angle$|$ langle$digit$ \_positive$$ 
angle$$ langle$digit$ \_string$$ 
angle$
    $ langle$digit$ \_string$$ 
angle$ ::= $ langle$digit$ 
angle$|$ langle$digit$ 
angle$$ langle$digit$ \_string$$ 
angle$
    $ langle$digit$ \_positive$$ 
angle$ ::= 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
    $ langle$digit$ 
angle$ ::= 0 | $ langle$digit$ \_positive$$ 
angle$
    $ langle$array$ \_name$$ 
angle$ ::= a | b | c | d | e | f | g | h | i | j | k | l | m |
        n | o | p | q | r | s | t | u | v | w | x | y | z |
        A | B | C | D | E | F | G | H | I | J | K | L | M |
        N | O | P | Q | R | S | T | U | V | W | X | Y | Z


    where $ langle$new$ \_line$$ 
angle$ denotes a new line character (LF).

    Characters used in a program are alphabetical letters, decimal digits, =, [, ] and new line characters. No other characters appear in a program.

    A declaration declares an array and specifies its length. Valid indices of an array of length n are integers between 0 and n - 1 , inclusive. Note that the array names are case sensitive, i.e. array a and array A are different arrays. The initial value of each element in the declared array is undefined.

    For example, array a of length 10 and array b of length 5 are declared respectively as follows.

    a[10] 
    b[5]
    

    An expression evaluates to a non-negative integer. A $ langle$number$ 
angle$ is interpreted as a decimal integer. An $ langle$array$ \_name$$ 
angle$ [ $ langle$expression$ 
angle$ ] evaluates to the value of the $ langle$expression$ 
angle$ -th element of the array. An assignment assigns the value denoted by the right hand side to the array element specified by the left hand side.

    Examples of assignments are as follows.

    a[0]=3 
    a[1]=0 
    a[2]=a[a[1]] 
    a[a[0]]=a[1]
    

     
    A program is executed from the first line, line by line. You can assume that an array is declared once and only once before any of its element is assigned or referred to.

    Given a program, you are requested to find the following bugs.

    • An index of an array is invalid.
    • An array element that has not been assigned before is referred to in an assignment as an index of array or as the value to be assigned.

    You can assume that other bugs, such as syntax errors, do not appear. You can also assume that integers represented by $ langle$number$ 
angle$ s are between 0 and 231 - 1(= 2147483647) , inclusive.

    Input 

    The input consists of multiple datasets followed by a line which contains only a single `.' (period).

    Each dataset consists of a program also followed by a line which contains only a single `.' (period).

    A program does not exceed 1000 lines. Any line does not exceed 80 characters excluding a new line character.

    Output 

    For each program in the input, you should answer the line number of the assignment in which the first bug appears. The line numbers start with 1 for each program. If the program does not have a bug, you should answer zero. The output should not contain extra characters such as spaces.

    Sample Input 

    a[3] 
    a[0]=a[1] 
    . 
    x[1] 
    x[0]=x[0] 
    . 
    a[0] 
    a[0]=1 
    . 
    b[2] 
    b[0]=2 
    b[1]=b[b[0]] 
    b[0]=b[1] 
    . 
    g[2] 
    G[10] 
    g[0]=0 
    g[1]=G[0] 
    . 
    a[2147483647] 
    a[0]=1 
    B[2] 
    B[a[0]]=2 
    a[B[a[0]]]=3 
    a[2147483646]=a[2] 
    . 
    .
    

    Sample Output 

    2 
    2 
    2 
    3 
    4 
    0

    题意很简单,就是找bug,可能数组的下标越界或未初始化;

    怎么说呢,一遇到不叫复杂的字符串题代码就越写越臭。到最后就崩溃了。这次还是找了网上的代码。更要命的是就连看人家的代码就看了一晚上。现在有些地方理解的也不透彻。技巧性比较强。自己果然需要加强这种题的锻炼。沉下心去写;
    废话不说了。上代码;
      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstdlib>
      4 #include<cstring>
      5 #include<string>
      6 #include<vector>
      7 #include<map>
      8 #include<cctype>
      9 #include<sstream> //stringstream
     10 using namespace std;
     11 typedef unsigned int uint;
     12 const int maxn = 1010;
     13 int dcnt, ans;
     14 bool bug;
     15 vector<string> vs;
     16 map<string, string> value;
     17 map<string, unsigned int> mdes;
     18 string get_val(string index, string name) //递归调用,返回下标的实数值(字符串形式)
     19 {
     20     uint v = 0;
     21     if(index.find('[') == index.npos) //直接是一个实数
     22     {
     23         stringstream ss(index); ss >> v;
     24         if(name != " " && mdes[name] <= v) //容易理解mdes[name] <= v表示数组的下标越界
     25                                            //而name == " "的情况为定义语句a[3];或赋值语句等号右值(这里理解的也不透彻。。。)
     26             bug = true;
     27         return index;
     28     }
     29     string pname, pindex;
     30     pname = index.substr(0, index.find('['));
     31     pindex = index.substr(index.find('[')+1, index.find_last_of(']')-2);
     32     pindex = get_val(pindex, pname);
     33     if(bug) return " ";
     34     string vv;
     35     vv = pname+"["+pindex+"]";
     36     if(!value.count(vv)) bug = true; //若下标未初始化,则bug
     37     return value[vv];
     38 }
     39 void Define(string str)
     40 {
     41     string name, index; uint v = 0;
     42     name = str.substr(0, str.find('['));//提取数组名
     43     index = str.substr(str.find('[')+1, str.find_last_of(']')-2); //提取数组下标
     44     index = get_val(index, " "); //此处" "意在区别赋值语句调用该函数的情况;
     45     stringstream ss(index);     ss >> v;
     46     mdes[name] = v;//存入数组name的范围
     47 }
     48 void Assign(string str)
     49 {
     50     string L, R, name, index; uint v = 0;
     51     L = str.substr(0, str.find('=')); R = str.substr(str.find('=')+1);
     52     /*等号左边*/
     53     name = L.substr(0, L.find('['));
     54     ///find_last_of();
     55     index = L.substr(L.find('[') + 1, L.find_last_of(']')-2);
     56     index = get_val(index, name);
     57     if(bug) {return;}
     58     stringstream ss(index);     ss >> v;
     59     if(v >= mdes[name]) {bug = true; return;}
     60     /*赋值*/
     61     string left_value, right_value;
     62     left_value = name+"["+index+"]";
     63     right_value = get_val(R, " ");
     64     value[left_value] = right_value;
     65 }
     66 void init()
     67 {
     68     mdes[" "] = 0;//初始化" "代表的数组大小为0,此主要为以后求数组下标值时区别赋值语句与定义语句(及赋值语句等号右边)。
     69     vs.clear();
     70     mdes.clear();
     71     value.clear();
     72 }
     73 int main()
     74 {
     75     //freopen("in.txt", "r", stdin);
     76     string str;
     77     while(cin >> str && str[0] != '.')
     78     {
     79         init();
     80         vs.push_back(str);
     81         while(cin >> str && str[0] != '.')
     82         {
     83             vs.push_back(str);
     84         }
     85         bug = false;
     86         for(int i = 0; i < vs.size(); i++)
     87         {
     88             if(vs[i].find('=') == vs[i].npos)
     89             {
     90                 Define(vs[i]);
     91             }
     92             else
     93                 Assign(vs[i]);
     94             if(bug)
     95             {
     96                 cout << i+1 << endl;
     97                 break;
     98             }
     99             if(!bug && i == vs.size()-1)
    100                 cout << 0 << endl;
    101         }
    102     }
    103     return 0;
    104 }

  • 相关阅读:
    pcDuino无显示器刷机与使用
    pcDuino安装vnc进行远程控制
    pcDuino 刷系统-卡刷
    HDU 5441 2015长春站online1005(并查集)
    HYSBZ 2002 Bounce 弹飞绵羊(分块)
    HYSBZ 2243 染色 LCT学习
    HYSBZ 2049 Cave 洞穴勘测
    SPOJ 375 LCT学习
    HDU 4010 动态树LCT学习
    ZJOI2008 树的统计 树链剖分学习
  • 原文地址:https://www.cnblogs.com/LLGemini/p/4306412.html
Copyright © 2011-2022 走看看