今天对着书敲的题目

https://cn.vjudge.net/problem/UVA-10815

Andy's First Dictionary

  • 使用set统计元素去重复
  • 使用stringstream,从string输入到stringstream再输入到string去空格
  • 使用tolower()函数小写化
  • 用sort()来排序
  • 用count()函数确认集合是否含有该元素
#include <iostream>
#include <stdio.h>
#include <iterator>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <string>
#include <sstream>

// #define LOCAL
#define ull unsigned long long int
#define ll long long int
#define ui unsigned int
#define fi first
#define se second

using namespace std;
set<string> dict;


int main() {
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	string s, buf;
	while(cin >> s) {
		for(int i = 0; i < s.size(); ++i) {
			if(isalpha(s[i])) {
				s[i] = tolower(s[i]);
			}
			else {
				s[i] = ' ';
			}
		}
		stringstream ss(s);
		while(ss >> buf) {
			dict.insert(buf);
		}
	}
	for(set<string>::iterator it = dict.begin(); it != dict.end(); ++it) {
		cout << *it << endl;
	}
	return 0;
}

 


https://cn.vjudge.net/problem/UVA-12096

The SetStack Computer

  • 使用map<set<int>, int>给不同的集合赋予一个唯一的ID,用ID带代替集合本身
  • 使用<algorithm>库里面的set_union()和set_intersection函数求并集和交集
  • 使用inserter迭代器进行插入操作,用inserter(x, x.begin())创建插入迭代器
#include <iostream>
#include <stdio.h>
#include <iterator>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <string>
#include <sstream>
#include <map>
#include <stack>

using namespace std;

// #define LOCAL
#define ull unsigned long long int
#define ll long long int
#define ui unsigned int
#define fi first
#define se second

#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
typedef set<int> Set;

map<Set, int> IDcache;
vector<Set> Setcache;
stack<int> stk;

int ID(Set x) {
	if (IDcache.count(x)) return IDcache[x];
	Setcache.push_back(x);
	return IDcache[x] = Setcache.size() - 1;
}



int main() {
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int t, n;
	cin >> t;
	while(t--) {
		cin >> n;
		while(n--) {
			string s;
			cin >> s;
			if		(s[0] == 'P') stk.push(ID(Set()));
			else if (s[0] == 'D') stk.push(stk.top());
			else {
				Set x1 = Setcache[stk.top()]; stk.pop();
				Set x2 = Setcache[stk.top()]; stk.pop();
				Set x;
				if (s[0] == 'U') set_union(ALL(x1), ALL(x2), INS(x));
				if (s[0] == 'I') set_intersection(ALL(x1), ALL(x2), INS(x));
				if (s[0] == 'A') {
					x = x2;
					x.insert(ID(x1));
				}
				stk.push(ID(x));
			}
			cout << Setcache[stk.top()].size() << endl;
		}
		cout << "***" << endl;
	}
	return 0;
}

 


https://cn.vjudge.net/problem/UVA-156

Ananagrams

  •  对字符串使用sort进行去排列化,使得同一个字符集合的排列(单词)都转换为相应的字母集合。
  • 使用map<string, int>对字符串进行计数
  • 依然使用map::count()确认集合里是否有该元素
#include <iostream>
#include <stdio.h>
#include <iterator>
#include <cmath>
#include <vector>
#include <algorithm>
#include <map>
#include <string>

// #define LOCAL
#define ull unsigned long long int
#define ll long long int
#define ui unsigned int
#define fi first
#define se second

using namespace std;

map<string, int> dict;
vector<string> ss;
vector<string> postss;
vector<string> ans;

void mapdeal(string s) {
	if (!dict.count(s)) dict[s] = 0;
	dict[s]++;
}

string stdstr(string s) {
	for (int i = 0; i < s.length(); ++i) {
		s[i] = tolower(s[i]);
	}
	sort(s.begin(), s.end());
	postss.push_back(s);
	return s;
}


int main() {
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	string s;
	while(cin >> s) {
		if(s == "#") {
			break;
		}
		ss.push_back(s);
		mapdeal(stdstr(s));
	}
	for(int i = 0; i < ss.size(); ++i) {
		if(dict[postss[i]] == 1) {
			ans.push_back(ss[i]);
		}
	}
	sort(ans.begin(), ans.end());
	for(vector<string>::iterator it = ans.begin(); it != ans.end(); ++it) {
		cout << *it << endl;
	}
	return 0;
}

https://cn.vjudge.net/problem/UVA-136

Ugly Numbers

  • 优先队列的妙用
  • 记住优先队列的比较是!cmp,不符合cmp的在上面。例如greater就是小顶堆。
#include <iostream>
#include <stdio.h>
#include <iterator>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <string>
#include <sstream>
#include <map>
#include <queue>

using namespace std;

// #define LOCAL
#define ull unsigned long long int
#define ll long long int
#define ui unsigned int
#define fi first
#define se second

priority_queue<ll, vector<ll>, greater<ll> > pq;
set<ll> s;
int co[] = { 2, 3, 5 };

int main() {
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	cout << "The 1500'th ugly number is ";
	pq.push(1);
	s.insert(1);
	for(int i = 1; ; ++i) {
		ll x = pq.top(); pq.pop();
		if(i == 1500) {
			cout << x << ".\n";
			break;
		}
		for (int j = 0; j < 3; ++j) {
			ll px = x * co[j];
			if (!s.count(px)) {
				s.insert(px);
				pq.push(px);
			}
		}
	}
	return 0;
}

 

https://cn.vjudge.net/problem/UVA-540

Team Queue

  • 映射思想,把一大块元素映射成一个元素(集合也可以作为元素),形成集合(集合的集合)。
  • 通过使用多个queue + 映射的方法实现大queue中有小queue的效果
#include <iostream>
#include <stdio.h>
#include <iterator>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <string>
#include <sstream>
#include <map>
#include <queue>

using namespace std;

// #define LOCAL
#define ull unsigned long long int
#define ll long long int
#define ui unsigned int
#define fi first
#define se second

const int maxt = 1e4 + 10;


int main() {
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int t, kase = 0;
	while(cin >> t && t) {
		queue<int> q, q2[maxt];
		cout << "Scenario #" << ++kase << endl;
		int n, x;
		map<int, int> team;
		for (int k = 0; k < t; ++k) {
			cin >> n;
			while(n--) {
				cin >> x;
				team[x] = k;
			}
		}

		string op;
		while(1) {
			cin >> op;
			if		(op[0] == 'S') break;
			else if (op[0] == 'E') {
				cin >> x;
				int xteam = team[x];
				if (q2[xteam].empty()) {
					q.push(xteam);
				}
				q2[xteam].push(x);
			}
			else if (op[0] == 'D') {
				int xteam = q.front();
				x = q2[xteam].front();
				q2[xteam].pop();
				if(q2[xteam].empty()) {
					q.pop();
				}
				cout << x << endl;
			}
		}
		cout << endl;
	}
	return 0;
}

 

https://cn.vjudge.net/problem/UVA-400

Unix ls

  • 记住公式:ceiling(n) = floor(n - 1) + 1 (证明略)
  • 按行输出却要保持列优先的输出顺序,怎么办?——推导出位于r行c列的元素应该是第n = f(c, r)个就行
  • 为了以防越界要加个判断 if (n < maxn)
  • 这个题解提供了给每一个字符串填充空格等排版字符的函数(print())
#include <iostream>
#include <stdio.h>
#include <iterator>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <string>
#include <sstream>
#include <map>
#include <queue>

using namespace std;

// #define LOCAL
#define ull unsigned long long int
#define ll long long int
#define ui unsigned int
#define fi first
#define se second

const int maxcol = 60;
vector<string> filenames;

void print(const string &s, int len, char extra) {
	cout << s;
	for(int i = 0; i < len - s.length(); ++i) {
		cout << extra;
	}
}


int main() {
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int n;
	while(cin >> n) {
		filenames.clear();
		int m = 0;
		for(int i = 0; i < n; ++i) {
			string s;
			cin >> s;
			filenames.push_back(s);
			m = max(m, (int)filenames[i].length());
		}
		// for rows: ceiling(n) = floor(n - 1) + 1
		int cols = (maxcol - m) / (m + 2) + 1, rows = (n - 1) / cols + 1;
		print("", 60, '-');
		cout << "\n";
		sort(filenames.begin(), filenames.end());
		for(int r = 0; r < rows; ++r) {
			for(int c = 0; c < cols; ++c) {
				int idx = c * rows + r;
				if (idx < n) print(filenames[idx], c == cols - 1 ? m : m + 2, ' ');
			}
			cout << "\n";
		}
	}
	return 0;
}

 

 


 

最后……原来vua里面Presentation Error算是Wrong Answer的……

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值