看完了這個代碼,發現,其實這個化學反應的過程並沒有很複雜
還是那種一個一個的方程式走,沒有使用所謂的拓撲排序。
map vector可以打印一下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
#include <map>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
using
namespace std;
char
ch[600]; vector< int > vec1[405],vec2[405];
map<string, int > m;
int
cnt=1; bool
b[2000],ne[2000],flag[405]; void
deal( char
*ch, int i)
{ char
str[15]; int
len=0,f=0; while ( 1 ){
sscanf (ch+len,
"%[^+=]" , str);
len +=
strlen (str);
if ( !m[str] ) m[str] = cnt++;
if ( !f ) vec1[i].push_back( m[str] );
else
vec2[i].push_back(m[str]); if ( *( ch+ (len) ) ==
'=' ) f = 1;
else
if (*(ch+len)== '\0' )
break ;
len ++;
}
} int
main(){ int
i,j,n,mm; scanf ( "%d" , &n);
for ( i = 0; i < n; i ++){
scanf ( "%s" , ch);
deal(ch,i);
}
scanf ( "%d" , &mm);
for ( i = 0; i < mm; i++){
scanf ( "%s" ,ch);
b[ m[ch] ] = 1;
}
int
f=1,num=0; while (f){
f = 0;
for ( i = 0; i < n; i ++){
if (flag[i])
continue ; ;
for ( j = 0, flag[i] = 1; j < vec1[i].size(); j ++)
if ( ! b[ vec1[i][j] ] ) flag[i] =0;
if (flag[i]){
for ( j = 0; j < vec2[i].size(); j ++)
if ( ! b[ vec2[i][j] ] ) {
b[ vec2[i][j] ] = 1;
ne[ vec2[i][j] ] = 1;
f = 1;
num++;
}
}
}
}
cout<<num<<endl;
for ( map<string, int >::iterator i = m.begin(); i !=m.end();
i ++) if ( ne[ (*i).second ] ) cout<<(*i).first<<endl;
} |