ZOJ 1244
7036 단어 ZOJ
Definite Values
Time Limit: 2 Seconds
Memory Limit: 65536 KB
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",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
// ,
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <cmath>
using namespace std;
int rc[30];
int main()
{
int n;
int t=1;
char a,b;
bool f;
int i;
while(scanf("%d",&n),n)
{
memset(rc,0,sizeof(rc));
rc[0]=1;
while(n--)
{
getchar();
scanf("%c = %c",&a,&b);
rc[a-'a']=rc[b-'a'];
}
f=0;
printf("Program #%d
",t++);
for(i=0;i<26;i++)
if(rc[i])
f=1,printf("%c ",i+'a');
if(!f)
printf("none");
printf("
");
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
POJ 2260 (ZOJ 1949) Error Correction 문제 하나A boolean matrix has the parity property when each row and each column has an even sum, i.e. contains an even number of ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.