Posted in Iterator

POJ 1002 487-3279

7/7/2017
Problem: http://poj.org/problem?id=1002
Solution: Map.
Note: It’s actually a rather easy one, but I overlooked the possibility of “000-
0000”. Also, the difference between head and resulted
in CE in C++.
It’s the first time iterating over a map, and the usage of “->first”
“->second”.



#include<iostream>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
map<string, int> mp;

char trans(char ch) {
	if (ch < 58) {
		return ch;
	} else {
		if (ch <= 'P') {
			return (char)((ch - 'A') / 3 + 2 + '0');
		} else {
			return (char)((ch - 'A' - 1) / 3 + 2 + '0');
		}		
	}
}

int main() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		// cout << "1" << endl;
		string s;
		cin >> s;
		// cout << s;
		string num;
		for (int j = 0; j < s.length(); j++) {
			if (s[j] != '-') {
				num += trans(s[j]);
			}
		}
		if (mp.find(num) != mp.end()) {
			mp[num] ++;
		} else {
			mp.insert(pair<string, int>(num, 1));
		}
	}
	map<string, int>::iterator it;
	bool b = false;
	for (it = mp.begin(); it != mp.end(); it++) {
		string tmp1 = it->first;
		int tmp2 = it->second;
		if (tmp2 > 1) {
			b = true;
			for (int i = 0; i <= 2; i++) {
				cout << tmp1[i];
			}
			cout << '-';
			for (int i = 3; i <= 6; i++) {
				cout << tmp1[i];
			}
			cout << ' ' << tmp2 << endl;
		}
	}
	if (!b) {
		cout << "No duplicates.";
	}
	return 0;
}

Posted in Iterator, Uncategorized

CF 755B PolandBall and Game

6/29/2017
Problem: http://codeforces.com/problemset/problem/755/B
Solution: It’s actually quite easy. If the equal string appears even times, then
simply compare n and m. Otherwise, n += 1 and compare n & m.
Note: The usage of iterator.
Under C++ 98, there’s no ‘for’ iterator. The only choice is literally
create an iterator.
To fetch the object of the iterator, simply put “*”.
The return value of find function is an iterator, but not a boolean value!
If it can be found then it’s the iterator, otherwise it’s set.end().



#include<iostream>
#include<set>
using namespace std;

int main() {
	int n, m;
	cin >> n >> m;
	set<string> a, b;
	for (int i = 1; i <= n; i++) {
		string s;
		cin >> s;
		a.insert(s);
	}
	for (int i = 1; i <= m; i++) {
		string s;
		cin >> s;
		b.insert(s);
	}
	int cnt = 0;
	set<string>::iterator it;
	for (it = b.begin(); it != b.end(); ++it) {
		if (a.find(*it) != a.end()) {
			cnt ++;
		}		
	}
	if (cnt % 2 == 0) {
		cout << (n > m ? "YES" : "NO");
		return 0;
	}
	n += cnt / 2 + 1;
	m += cnt / 2;
	cout << (n > m ? "YES" : "NO");
	return 0;
}