HDU1327 ZOJ1244 Definite Values水题

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU1327 ZOJ1244 Definite Values水题相关的知识,希望对你有一定的参考价值。

Definite Values
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1877 Accepted Submission(s): 733

Problem Description
A common error in programming is to use variables that have not been initialized before. For example, in C and C++, all variables have an indefinite value after declaration - their value can be anything. Thus, the following program

main()
{
int x;
printf("%d\\n",x);
}

could print any number. But even in languages such as Pascal, where all values are initialized to zero, it is useful to give variables definite values before using them, the avoid side effects when your code portion is placed into a different context.

Generally, the problem of deciding for a given program whether all variables have been assigned values before they are read out, is undecidable. But if you, as in this problem, consider only a sequence of assignments, the problem becomes solvable.

Input
The input contains several program parts. Each part starts with a number n on a line by itself, the number of lines in the program part. The following n lines contain each an assignment of the form “variable1 = variable2”, where the variablei’s are lower-case letters.

The input is terminated by a test case starting with n = 0.

Output
Assume that before the execution of the given program part, variable a has some definite value, while all other variables are undefined. You have to print the names of the varaibles which have a definite value after the execution of the program part. More specifically, format your output as follows.

For each program part in the input, first print the number of the program, as shown in the sample output. Then print a line containing the names of the variables which have a definite value after the execution of the given program part. Print them in alphabetically sorted order, and leave one blank after each variable name. If none of the variables has a definite value after the execution of the program part, print the word “none”.

Print a blank line after each test case.

Sample Input
4
b = a
c = d
d = b
e = f
1
a = b
0

Sample Output
Program #1
a b d

Program #2
none

Source
Southwestern Europe 1997, Practice

问题链接HDU1327 ZOJ1244 Definite Values
问题简述:判定程序中的变量其值是否被初始化(赋值)?变量a被初始化过。
问题分析:简单问题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* HDU1327 ZOJ1244 Definite Values */

#include <bits/stdc++.h>

using namespace std;

const int N = 10;
char s[N];
int mp[256];

int main()
{
    int n, caseno = 0;
    while (~scanf("%d", &n) && n) {
        memset(mp, 0, sizeof mp);
        mp['a'] = 1;
        for (int i = 1; i <= n; i++) {
            fgets(s, N, stdin);
            while(s[0] == '\\n') fgets(s, N, stdin);
            mp[(int)s[0]] = mp[(int)s[4]];
        }

        int flag = 0;
        printf("Program #%d\\n", ++caseno);
        for (char c = 'a'; c <= 'z'; c++)
            if (mp[(int)c] > 0) {
                flag = 1;
                printf("%c ", c);
            }
        if (flag == 0) printf("none");
        printf("\\n\\n");
    }

    return 0;
}

以上是关于HDU1327 ZOJ1244 Definite Values水题的主要内容,如果未能解决你的问题,请参考以下文章

HDU 3783 ZOJ

HDU1320 ZOJ1201 Inversion水题

HDU1217 POJ2240 ZOJ1092 ArbitrageFloyd算法

HDU1328 ZOJ1240 IBM Minus One水题

HDU1887 ZOJ2982 UVALive3958 Weird Numbers进制

POJ2033 LA3078 HDU1508 ZOJ2202 AlphacodeDFS+DP