NYOJ542-试制品

题目链接:点击打开链接

 

试 制 品

时间限制:1000 ms  |  内存限制:65535 KB

难度:4

描述

ZZ大学的Dr.Kong最近发现实验室的很多试制品都已经用完。由于项目经费有限,为了节省,Dr.Kong决定利用实验室现有的试制品来生成所缺的试制品。为此,Dr.Kong连续几天通宵达旦整理出一份研究资料并让研究生Bill去实验并统计能产生多少种所缺的试制品。

Bill从头到尾翻完所有的资料,发现资料上写满了一大堆的化学方程式,上面除了大小写英文字母、数字、加号、等号外,再也没有其他的符号了。其中,每个方程式都是A1+A2+……+Ap=B1+B2+……+Bq的形式, 表示试制品A1,A2,……和Ap反应,生成了试制品B1,B2,……,Bq。其中Ai和Bj都是一种单质或化合物的化学式(长度不超过10个字符),1≤p,q ≤ 20 。每个方程式的总长不超过100个字符。有些试制品的化学式可能在现代社会的化学元素周期表里找不到,这是由于化学反应过程中可能又有物理反应导致的结果。

Bill头疼了,从哪个实验开始呢?你能帮助他吗?

输入

有多组测试数据。
第一行:N表示Dr.Kong写的化学方程式个数(1<=N<=400)
接下来有N行, 每一行是一个方程式.
再接下来的一行:M表示已有多少种试制品.(1<=M<=500)
接下来有M行,每一行是已有的一种试制品的化学式.

输出

第一行包含一个数T,表示可以产生多少种所缺的试制品.
在接下来的T行中,按ASCII码升序输出产生的试制品的化学式.

样例输入

4H2O+Na=NaOH+H2Cl2+H2=HClFe+O2=Fe3O4NaOH+HCl=H2O+NaCl3H2ONa	Cl2

样例输出

4H2HClNaClNaOH

题目大意:给几个方程式,还有起始反应物,然后跑方程式,如果反应物都有,那么可以得到生成物,同时也可以作为反应物。直到没有新的生成物,输出除了最开始的反应物的生成物。

思路:这道题第一次写理解错了题意。这次写直接用两个set容器,然后循环100遍。输出结果就可以了。

AC代码:

#include<bits/stdc++.h>
using namespace std;

int n, m;
vector<string> t;
string s;
set<string> scp;
set<string> yl;

int main() {
	while(~scanf("%d", &n)) {
		t.clear();
		scp.clear();
		yl.clear();
		for(int i = 0; i < n; i++) {//方程式
			cin >> s;
			t.push_back(s);			
		}
		scanf("%d", &m);
		while(m--) {
			cin >> s;
    			yl.insert(s);//原料
		}
		int xh = 100;
		while(xh--) {
			for(int i = 0; i < n; i++) {
				string temp = t[i];
				string temptrue;
				int flag = 0;
				int j;
				for(j = 0; j < temp.length(); j++) {//判断原料是否具备
					if(temp[j] == '=') {
//					cout <<  "***" << temptrue  << "***" << endl;
						if(yl.find(temptrue) == yl.end()) {
							flag = 1;
						} else temptrue.clear();
						break;
					}
					if(temp[j] == '+') {
//						cout <<  "***" << temptrue  << "***" << endl;
						if(yl.find(temptrue) == yl.end()) {
							flag = 1;
							break;
						} else temptrue.clear();
					} else temptrue += temp[j];
				}
				if(flag == 0) {//如果具备
					for(int k = j+1; k < temp.length(); k++) {
						if(temp[k] == '+') {
//							cout <<  "***" << temptrue  << "***" << endl;
							if(yl.find(temptrue) == yl.end())//原料里没有就是新生成的
								scp.insert(temptrue);
							yl.insert(temptrue);//作为原料放入
							temptrue.clear();
						} else if(k == temp.length() - 1) {
							temptrue += temp[k];
//							cout <<  "***" << temptrue  << "***" << endl;
							if(yl.find(temptrue) == yl.end())
								scp.insert(temptrue);
							yl.insert(temptrue);
							temptrue.clear();
						} else temptrue += temp[k];
					}
				}
			}
		}
		printf("%d\n", scp.size());
		set<string>::iterator it = scp.begin();
		for(; it != scp.end(); it++) {
			cout << *it << endl;
		}
	}
}

 

posted @ 2018-07-13 21:13  Frontierone  阅读(121)  评论(0编辑  收藏  举报