programming-challenges Erdös Numbers (110206) 题解

主要是对各种输入字符串的解析,其实不是很喜欢,不过也算练了基本功了。关于输入的各种处理要注意的问题,网上都可以找到,就不详细记载了,反正是挺讨厌的,但是从积极的方面想,这样才是我们在真实世界中经常要处理的情况吧?后来发现一个自己一直没有认识到的错误,vector.resize()不会做reset,以前在vector中的值还是被保留了。另外学了一个新的reset vector的方法,fill(vector.begin(), vector.end(), 0); 

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <assert.h>
#include <algorithm>
#include <math.h>
#include <ctime>
#include <functional>
#include <string.h>
#include <stdio.h>
#include <numeric>
#include <float.h>

using namespace std;

const int DIMENTION = 10010; 
vector<int> G[DIMENTION]; 
vector<int> erdosNum(DIMENTION); 
int authorNum = 0;

void wfs(int node) {
	queue<int> que; 
	que.push(node); 
	vector<bool> visited(DIMENTION); 
	visited[node] = true;
	erdosNum[node] = 0; 

	while (!que.empty()) {
		int node = que.front(); que.pop();
		for (int i = 0; i < G[node].size(); i++) {
			if (!visited[G[node][i]]) {
				visited[G[node][i]] = true;
				erdosNum[G[node][i]] = erdosNum[node] + 1; 
				que.push(G[node][i]); 
			}
		}
	}
}

int main() {
	int TC; cin >> TC; 
	for (int tc = 1; tc <= TC; tc++) {
		// init
		for (int i = 0; i < DIMENTION; i++) {
			G[i].clear();
		}

		fill(erdosNum.begin(), erdosNum.end(), 0);
		authorNum = 0;

		int m, n; cin >> m >> n; getchar();
		map<string, int> strToNodes;
		vector<string> author(n);
		vector<string> paper(m); 
		 
		for (int i = 0; i < m; i++) getline(cin, paper[i]);
		for (int i = 0; i < n; i++) getline(cin, author[i]);

		// build graph
		for (int i = 0; i < m; i++) {
			string authors = paper[i].substr(0, paper[i].find_first_of(':'));
			vector<int> allNodes; 
			
			int pos = authors.rfind(".,"); 
			while (pos != string::npos) {
				string oneAuthor = authors.substr(pos+3); 
				if (strToNodes.find(oneAuthor) == strToNodes.end()) {
					allNodes.push_back(authorNum);
					strToNodes[oneAuthor] = authorNum; 
					authorNum++; 
				}
				else {
					allNodes.push_back(strToNodes[oneAuthor]); 
				}
				authors.erase(pos+1); 

				pos = authors.rfind(".,");
			}
			if (strToNodes.find(authors) == strToNodes.end()) {
				allNodes.push_back(authorNum);
				strToNodes[authors] = authorNum;
				authorNum++;
			}
			else {
				allNodes.push_back(strToNodes[authors]);
			}

			for (int j = 0; j < allNodes.size(); j++) {
				for (int k = j + 1; k < allNodes.size(); k++) {
					G[allNodes[j]].push_back(allNodes[k]);
					G[allNodes[k]].push_back(allNodes[j]);
				}
			}
		}

		wfs(strToNodes["Erdos, P."]);
		cout << "Scenario " << tc << endl; 
		for (int i = 0; i < author.size(); i++) {
			int num = 0; 
			if (strToNodes.find(author[i]) != strToNodes.end())
				num = erdosNum[strToNodes[author[i]]];
			if (author[i] == "Erdos, P.") {
				cout << author[i] << " 0" << endl;
			}
			else if (num == 0) {
				cout << author[i] << " infinity" << endl; 
			}
			else {
				cout << author[i] << " " << num << endl; 
			}
		}
	}

	return 0; 
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值