解析:
主要是char string 与 int 的转换
用map即可
构造出关系矩阵就好了
#include <bits/stdc++.h> using namespace std; map<string, int> index; map<string, int> hashmap; map<int, string> unhashmap; map<string, int> vis; vector<string> mm; vector<int> cores[1010]; int line[1010][1010], girl[1010]; int n, m; int cnt; int used[1010]; int main() { memset(line, 0, sizeof(line)); cin >> n >> m; cnt = 0; for(int i = 0; i < n; i++) { string u, v; cin >> u >> v; int x, y; if(hashmap[u] == 0) { hashmap[u] = ++cnt; x = cnt; } else x = hashmap[u]; if(hashmap[v] == 0) { hashmap[v] = ++cnt; y = cnt; } else y = hashmap[v]; unhashmap[y] = v; unhashmap[x] = u; cores[x].push_back(y); cores[y].push_back(x); } for(int i = 0; i < m; i++) { string temp; cin >> temp; mm.push_back(temp); vis[temp] = 1; } cnt = 0; for(int i = 0; i < m; i++) { string name, xing = "", ming = ""; name = mm[i]; for(int j = 0; j < name.length(); j++) { if(name[j] >= 'a' && name[j] <= 'z') ming += name[j]; else xing += name[j]; } int x, y; if(index[name] == 0) { index[name] = ++cnt; x = cnt; } else x = index[name]; int id = hashmap[ming]; for(int j = 0; j < cores[id].size(); j++) { string yname = xing + unhashmap[cores[id][j]]; if(vis[yname] == 0) continue; if(index[yname] == 0) { index[yname] = ++cnt; y = cnt; } else y = index[yname]; line[x][y] = 1; line[y][x] = 1; } } int ans = 0; for(int i = 1; i <= cnt; i++) { for(int j = 1; j <= cnt; j++) if(line[i][j] == 1) ans++; } cout << ans / 2 << endl; return 0; }