CF1156G.Optimizer
省选/NOI-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Let's analyze a program written on some strange programming language. The variables in this language have names consisting of 1 to 4 characters, and each character is a lowercase or an uppercase Latin letter, or a digit. There is an extra constraint that the first character should not be a digit.
There are four types of operations in the program, each denoted by one of the characters: $, ^, # or &.
Each line of the program has one of the following formats:
= , where and are valid variable names; = , where , and are valid variable names, and is an operation character.
The program is executed line-by-line, and the result of execution is stored in a variable having the name res. If res is never assigned in the program, then the result will be equal to the value of res before running the program.
Two programs are called equivalent if no matter which operations do characters $, ^, # and & denote (but, obviously, performing the same operation on the same arguments gives the same result) and which values do variables have before execution of program, the value of res after running the first program is equal to the value of res after running the second program (the programs are executed independently).
You are given a program consisting of n lines. Your task is to write a program consisting of minimum possible number of lines that is equivalent to the program you are given.
我们来分析一段用某种奇怪编程语言编写的程序。该语言中的变量名由 1 至 4 个字符组成,每个字符为小写或大写拉丁字母,或数字;此外还有一个额外约束:首字符不能是数字。
程序中包含四种操作,分别用字符 $, ^, # 或 & 表示。
程序的每一行具有以下两种格式之一:
<lvalue>=<rvalue>,其中<lvalue>和<rvalue>均为合法的变量名;<lvalue>=<arg1><op><arg2>,其中<lvalue>、<arg1>和<arg2>均为合法的变量名,且<op>是一个操作字符。
程序按行顺序执行,执行结果存储在名为 res 的变量中。若程序中从未对 res 进行赋值,则执行后的结果等于程序运行前 res 的值。
若对于任意对字符 $, ^, # 和 & 所表示的操作的定义(显然,对相同参数执行相同操作所得结果也相同),以及任意程序执行前各变量的初始值,两个程序执行后 res 的值均相等(两个程序独立执行),则称这两个程序等价。
现给定一个包含 n 行的程序。你的任务是编写一个与之等价、且行数最少的程序。
输入格式
The first line contains one integer n (1≤n≤1000) — the number of lines in the program.
Then n lines follow — the program itself. Each line corresponds to the format described in the statement and has no extra whitespaces.
第一行包含一个整数 n(1≤n≤1000)——程序中的行数。
接下来是 n 行——程序本身。每行均符合题目描述中的格式,且不含额外的空白字符。
输出格式
In the first line print k — the minimum number of lines in the equivalent program.
Then print k lines without any whitespaces — an equivalent program having exactly k lines, in the same format it is described in the statement.
第一行输出 k —— 等价程序所需的最少行数。
随后输出 k 行,每行不含任何空白字符——即一个恰好包含 k 行的等价程序,其格式与题目描述中一致。
输入输出样例
输入#1
4 c=aa#bb d12=c res=c^d12 tmp=aa$c
输出#1
2 aaaa=aa#bb res=aaaa^aaaa
输入#2
2 max=aaaa$bbbb min=bbbb^aaaa
输出#2
0
输入解题思路,AI测评打分。不知道怎么写?