⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 1284.cpp

📁 这是哈尔滨工业大学acmOJ的源代码
💻 CPP
字号:
/*  This Code is Submitted by wywcgs for Problem 1284 on 2006-01-12 at 02:04:08 */ 
#include <cstdio>
#include <cstring>
#include <cctype>
#include <map>
using namespace std;

const int MAX = 256;

struct cmp {
	bool operator ()(const char* s1, const char* s2) const {
		return strcmp(s1, s2) < 0;
	}
};

map<const char*, int, cmp> atom;

int weight();

int main()
{
	char atomic[MAX][32], line[MAX], ch;
	int n, w;
	
	atom.clear();
	for(n = 0; scanf("%s\n", atomic[n]) != EOF 
		&& strcmp(atomic[n], "END_OF_FIRST_PART"); n++) {
		scanf("%d\n", &w);
		atom[atomic[n]] = w;
	}
	while((ch = getchar()) != '0') {
		ungetc(ch, stdin);
		w = weight();
		if(w == -1) printf("UNKNOWN\n"), gets(line);
		else printf("%d\n", w);
	}
	
	return 0;
}

int weight()
{
	char ato[4] = { 0 };
	int w = 0, pw, n;
	while((ato[0] = getchar()) != '\n' && ato[0] != ')') {
		if(ato[0] == '(') {
			if((pw = weight()) == -1) return -1;
			scanf("%d", &n);
		} else {
			ato[1] = getchar();
			if(!islower(ato[1])) ungetc(ato[1], stdin), ato[1] = 0;
			if(atom.count(ato) == 0) return -1;
			else pw = atom.find(ato)->second;
			ato[3] = getchar(); ungetc(ato[3], stdin);
			if(isdigit(ato[3])) scanf("%d", &n);
			else n = 1;
		}
		w += pw * n;
	}
	return w;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -