NYIST 542 - 试制品 (STL + 小暴力)

思路

用map先把各种反应物生成物编号,然后不断扫描反应物,如果有一个式子的反应物充足,记下生成物,继续扫描。

直到扫描完全部的反应式没有生成物生成。

我用了两个数组,分别存反应物和生成物。然后各种STL。。。

详情见代码

代码

#include <cstdio>
#include <stack>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <cmath>
#define LL long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(arr, num) memset(arr, num, sizeof(arr))
#define PB push_back
#define F first
#define S second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define LC rt << 1, l, mid
#define RC rt << 1|1, mid + 1, r
#define LRT rt << 1
#define RRT rt << 1|1
#define BitCount(x) __builtin_popcount(x)
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
using namespace std;
const int MAXN = 1000 + 10;
const int MOD = 1e9 + 7;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
 
typedef pair<int, int> pii;
typedef vector<int>::iterator VITI;
typedef vector<pii>::iterator VITII;
 
map<int, string> rmp;
map<string, int> mp;
vector<int> react[MAXN], result[MAXN];
vector<string> out;
int tot, vis[MAXN], add[MAXN], ans;
 
void Solve()
{
    int n;
    scanf("%d", &n);
    string tmp;
    for (int i = 0; i < n; i++)
    {
        cin >> tmp;
        if (mp.count(tmp)) vis[mp[tmp]] = 1;
    }
    while (true)
    {
        bool flag = false;
        VITI it;
        for (int i = 1; i <= tot; i++)
        {
            if (add[i]) continue;   //如果已经统计过的跳过
            for (it = react[i].begin(); it != react[i].end(); it++)
                if (!vis[*it]) break;
            if (it == react[i].end())   //说明反应物充足,接下来统计生成物
            {
                for (VITI iti = result[i].begin(); iti != result[i].end(); iti++)
                    if (!vis[*iti])
                    {
                        vis[*iti] = 1;
                        ans++;
                        out.PB(rmp[*iti]);
                    }
                add[i] = 1; flag = true;
            }
        }
        if (!flag) break;
    }
    sort(out.begin(), out.end());
    cout << ans << endl;
    for (int i = 0; i < SZ(out); i++) cout << out[i] << endl;
}
 
void Init(int n)
{
    MS(vis, 0); MS(add, 0);
    mp.clear(), rmp.clear();
    out.clear();
    for (int i = 0; i <= n; i++)
    {
        react[i].clear();
        result[i].clear();
    }
}
 
char str[110];
 
int main()
{
    //ROP;
    int n, i, j;
    while (~scanf("%d", &tot))
    {
        Init(tot);
        int number = 0;
        for (i = 1; i <= tot; i++)
        {
            scanf("%s", str);
            string tmp;
            int cnt = 0;
            for (j = 0; j < strlen(str); j++)
            {
                if (str[j] == '=')
                {
                    cnt = 1;
                    if (!mp.count(tmp))
                    {
                        mp[tmp] = number;
                        rmp[number++] = tmp;
                    }
                    react[i].PB(mp[tmp]);
                    tmp.clear();
                    continue;
                }
                if (cnt == 0)
                {
                    if (str[j] != '+') tmp += str[j];
                    else
                    {
                        if (!mp.count(tmp))
                        {
                            mp[tmp] = number;
                            rmp[number++] = tmp;
                        }
                        react[i].PB(mp[tmp]);
                        tmp.clear();
                    }
                }
                else
                {
                    if (str[j] != '+') tmp += str[j];
                    else
                    {
                        if (!mp.count(tmp))
                        {
                            mp[tmp] = number;
                            rmp[number++] = tmp;
                        }
                        result[i].PB(mp[tmp]);
                        tmp.clear();
                    }
                }
            }
            if (!mp.count(tmp))
            {
                mp[tmp] = number;
                rmp[number++] = tmp;
            }
            result[i].PB(mp[tmp]);
        }
        Solve();
    }
    return 0;
}

Powered by Jekyll and Theme by solid