ZOJ 1244 Definite Values

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

假设在给定程序部分的执行之前,变量A有一定的值,而其他所有变量都是未定义的。
需要判断之后哪几个变量有值。

题目不难,输入那块有些坑。。。。一般我们都是写a=b,而题目给的是a = b 

这两个空格还真的不容易看出来。。

AC代码:

#include <stdio.h>
int main()
{
    int cnt=1;
    int a[26];
    int n;
    while(scanf("%d",&n)&&n)
    {
        int i;
        for(i=0; i<26; i++)
            a[i]=0;
        a[0]=1;//a有初值
        char v1,v2;
        for(i=0; i<n; i++)
        {
            getchar();//关键,消去每次换行的影响
            scanf("%c = %c",&v1,&v2);
           //printf("%c %c",v1,v2);
            int x1,x2;
            x1=v1-'a',x2=v2-'a';
            //printf("%d %d",x1,x2);
            a[x1]=a[x2];
        }
        printf("Program #%d\n",cnt++);
        int q=0;
        for(i=0; i<26; i++)
            if(a[i])
            {
                q++;
                printf("%c ",(char)(i+'a'));
            }
        if(q==0)
            printf("none");
        printf("\n\n");
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黎曼猜想·

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值