TZOJ--1264: Definite Values(模拟)

1264: Definite Values 

时间限制(普通/Java):1000MS/10000MS     内存限制:65536KByte

描述

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.

输入

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.

输出

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.

样例输入

 

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

样例输出

Program #1
a b d

Program #2
none

题目来源

SW Europe 1997

 

题目链接:http://tzcoder.cn/acmhome/problemdetail.do?&method=showdetail&id=1264

 

 

题意:开始a是存在初始值的,然后执行一下赋值操作,问最后有多少变量有赋值

 

开始以为这种题目是并查集的基本题目,后来发现他不用整个合并过去,只需要修改单个值既可以了,map模拟一下,开始初始化mp["a"]=true,然后输入字符串时就用mp[s1]=mp[s2]

 

#include<bits/stdc++.h>
using namespace std;
int main(){
    int t,rs=0;
    string a,b,c;
    while(cin>>t,t){
        ++rs;
        cout<<"Program #"<<rs<<endl;
        map<string,bool>mp;
        mp.clear();
        mp["a"]=true;
        while(t--){
            cin>>a>>b>>c;
            mp[a]=mp[c];
        }
        int s=0;
        for(map<string,bool>::iterator it=mp.begin();it!=mp.end();++it){
            if(it->second){
                cout<<it->first<<" ";
                ++s;
            }
        }
        if(s==0){
            cout<<"none"<<endl;
        }else cout<<endl;
        cout<<endl;
    }
}

  

 

posted on 2018-11-28 15:25  Anidlebrain  阅读(180)  评论(0编辑  收藏  举报

导航