| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
RE了 .... see one see look one look ... i'm so puzzled ...In Reply To:我用gets+sscanf搞掂…… Posted by:frkstyc at 2006-03-27 12:01:46 #include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <map>
using namespace std;
const string GOAL = "Sorcerer's Stone";
const int MaxLen = 100;
map<string, vector<string> > stone;
map<string, int> exist, flag, root;
int ans, lev;
void search(string key) {
int key_lev, son_lev;
root[key] = flag[key] = lev++;
if (exist.find(key) == exist.end()) {
ans ++;
exist[key] = 1;
}
if (stone[key].empty()) {
return;
}
for (int i = 0; i < (int) stone[key].size(); i ++) {
string son = stone[key][i];
key_lev = root[key];
if (flag.find(son) != flag.end()) {
root[key] = key_lev < flag[son] ? key_lev : flag[son];
}
else {
search(son);
if (ans == -1) return;
son_lev = root[son];
root[key] = key_lev < son_lev ? key_lev : son_lev;
}
}
if (root[key] < flag[key]) {
ans = -1;
}
}
void getstones(char *names, vector<string> &stones) {
int len = (int) strlen(names), i;
string name;
stones.clear();
for (i = 0; i < len && names[i] != ':'; i ++) {
if (names[i] == ',') {
stones.push_back(name);
name.clear();
i ++;
continue;
}
name.push_back(names[i]);
}
stones.push_back(name);
name.clear();
for (i += 2; i < len; i ++) {
name.push_back(names[i]);
}
stone[name] = stones;
}
int main() {
// freopen("Stone_in.txt", "r", stdin);
int n, m;
char names[MaxLen], buf[MaxLen];
vector<string> stones;
while (scanf("%d%d", &n, &m), n && m) {
gets(buf);
ans = lev = 0;
stone.clear();
exist.clear();
flag.clear();
root.clear();
for (int i = 0; i < n; i ++) {
gets(names);
exist[string(names)] = 1;
}
for (int i = 0; i < m; i ++) {
gets(names);
getstones(names, stones);
}
search(GOAL);
printf("%d\n", ans);
}
return 0;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator