[HIHO]hihoCoder太阁最新面经算法竞赛7
题目链接:http://hihocoder.com/contest/hihointerview12
期末完事了,终于有时间成套刷题了。这套题比较简单,难度上感觉和上一套差不多。除了最后一个题是看了讨论版说数据水才敢写的。
A Word Construction
解法:正解应该是dfs+剪枝,我的思路是贪心,竟然过了。先把字符串按字典序排序,然后枚举每一个串作为起始,记下串内有什么字符,再从头到尾枚举所有字符串,看看每一个是否符合条件。就那么100个字符串,数据弱得很。
- #include <algorithm>
- #include <iostream>
- #include <iomanip>
- #include <cstring>
- #include <climits>
- #include <complex>
- #include <cassert>
- #include <cstdio>
- #include <cstdlib>
- #include <bitset>
- #include <vector>
- #include <deque>
- #include <queue>
- #include <stack>
- #include <ctime>
- #include <set>
- #include <map>
- #include <cmath>
- using namespace std;
- #define fr first
- #define sc second
- #define cl clear
- #define BUG puts("here!!!")
- #define W(a) while(a--)
- #define pb(a) push_back(a)
- #define Rint(a) scanf("%d", &a)
- #define Rll(a) scanf("%I64d", &a)
- #define Rs(a) scanf("%s", a)
- #define Cin(a) cin >> a
- #define FRead() freopen("in", "r", stdin)
- #define FWrite() freopen("out", "w", stdout)
- #define Rep(i, len) for(int i = 0; i < (len); i++)
- #define For(i, a, len) for(int i = (a); i < (len); i++)
- #define Cls(a) memset((a), 0, sizeof(a))
- #define Clr(a, x) memset((a), (x), sizeof(a))
- #define Full(a) memset((a), 0x7f7f7f, sizeof(a))
- #define lrt rt << 1
- #define rrt rt << 1 | 1
- #define pi 3.14159265359
- #define RT return
- #define lowbit(x) x & (-x)
- #define onenum(x) __builtin_popcount(x)
- typedef long long LL;
- typedef long double LD;
- typedef unsigned long long ULL;
- typedef pair<int, int> pii;
- typedef pair<string, int> psi;
- typedef pair<LL, LL> pll;
- typedef map<string, int> msi;
- typedef vector<int> vi;
- typedef vector<LL> vl;
- typedef vector<vl> vvl;
- typedef vector<bool> vb;
- const int maxn = ;
- string s[maxn];
- int n;
- int ascii[];
- int main() {
- // FRead();
- while(~Rint(n)) {
- Rep(i, n) cin >> s[i];
- sort(s, s+n);
- int ret = ;
- Rep(k, n) {
- Cls(ascii);
- Rep(j, s[k].length()) ascii[s[k][j]] = ;
- int cur = ;
- Rep(i, n) {
- bool flag = ;
- Rep(j, s[i].length()) {
- if(ascii[s[i][j]] == ) {
- flag = ;
- break;
- }
- }
- if(flag == ) {
- Rep(j, s[i].length()) ascii[s[i][j]] = ;
- cur++;
- }
- }
- ret = max(ret, cur);
- }
- printf("%d\n", ret);
- }
- RT ;
- }
A
B Email Merge
解法:STL+并查集,这题很好想,但是写起来比较麻烦。对于字符串数组,想要做并查集操作的话,需要map<string, int>这样从字符串到整型的映射支持。读入数据按照邮箱为根节点,儿子是用户名,构建一个森林。这个时候同时更新并查集,也就是合并同一个树根下的所有用户名字符串。接着遍历所有用户名,扔到对应的belong中。我开的belong是堆,因为输出要求按照出现的顺序,所以堆序就是输入的顺序,也就是之前赋值的id号。
- #include <algorithm>
- #include <iostream>
- #include <iomanip>
- #include <cstring>
- #include <climits>
- #include <complex>
- #include <cassert>
- #include <cstdio>
- #include <cstdlib>
- #include <bitset>
- #include <vector>
- #include <deque>
- #include <queue>
- #include <stack>
- #include <ctime>
- #include <set>
- #include <map>
- #include <cmath>
- using namespace std;
- #define fr first
- #define sc second
- #define cl clear
- #define BUG puts("here!!!")
- #define W(a) while(a--)
- #define pb(a) push_back(a)
- #define Rint(a) scanf("%d", &a)
- #define Rll(a) scanf("%I64d", &a)
- #define Rs(a) scanf("%s", a)
- #define Cin(a) cin >> a
- #define FRead() freopen("in", "r", stdin)
- #define FWrite() freopen("out", "w", stdout)
- #define Rep(i, len) for(int i = 0; i < (len); i++)
- #define For(i, a, len) for(int i = (a); i < (len); i++)
- #define Cls(a) memset((a), 0, sizeof(a))
- #define Clr(a, x) memset((a), (x), sizeof(a))
- #define Full(a) memset((a), 0x7f7f7f, sizeof(a))
- #define lrt rt << 1
- #define rrt rt << 1 | 1
- #define pi 3.14159265359
- #define RT return
- #define lowbit(x) x & (-x)
- #define onenum(x) __builtin_popcount(x)
- typedef long long LL;
- typedef long double LD;
- typedef unsigned long long ULL;
- typedef pair<int, int> pii;
- typedef pair<string, int> psi;
- typedef pair<LL, LL> pll;
- typedef map<string, int> msi;
- typedef vector<int> vi;
- typedef vector<LL> vl;
- typedef vector<vl> vvl;
- typedef vector<bool> vb;
- const int maxn = ;
- typedef struct Node {
- string d;
- int idx;
- Node() { idx = -; }
- Node(string dd, int ii) : d(dd), idx(ii) {}
- friend bool operator<(Node a, Node b) { return a.idx > b.idx; }
- }Node;
- string mail, user;
- bool vis[maxn];
- map<string, int> id;
- map<string, set<string> > wt;
- priority_queue<Node> belong[];
- int n, m, cnt;
- int pre[maxn];
- int find(int x) {
- return x == pre[x] ? x : pre[x] = find(pre[x]);
- }
- void unite(int x, int y) {
- x = find(x); y = find(y);
- if(x < y) pre[y] = x;
- else pre[x] = y;
- }
- int main() {
- // FRead();
- cnt = ;id.cl(); wt.cl(); Cls(vis);
- Rint(n);
- Rep(i, n*) {
- pre[i] = i;
- while(!belong[i].empty()) belong[i].pop();
- }
- Rep(i, n) {
- cin >> user;
- cin >> m;
- id[user] = cnt++;
- Rep(j, m) {
- cin >> mail;
- if(id.find(mail) == id.end()) id[mail] = cnt++;
- wt[mail].insert(user);
- unite(id[user], id[mail]);
- }
- }
- map<string, set<string> >::iterator it;
- set<string>::iterator each;
- for(it = wt.begin(); it != wt.end(); it++) {
- for(each = it->second.begin(); each != it->second.end(); each++) {
- if(!vis[id[*each]]) {
- vis[id[*each]] = ;
- belong[find(id[*each])].push(Node(*each, id[*each]));
- }
- }
- }
- Rep(i, cnt) {
- if(!belong[i].empty()) {
- while(!belong[i].empty()) {
- cout << belong[i].top().d << " ";
- belong[i].pop();
- }
- cout << endl;
- }
- }
- RT ;
- }
B
C Matrix Sum
解法:这题毒瘤题…数据里有负数,不同语言对负数取模运算的结果不一样,C++和Java是一个负数对一个正数取模是一个负数,而Python是正数。这题的正解应当是二维线段树or树状数组(树套树),分别维护自己列的值。而我直接维护前缀和了。写了各种语言的解法,改来改去最后改了java。
- import java.lang.reflect.Array;
- import java.util.Arrays;
- import java.util.Comparator;
- import java.util.Scanner;
- import javax.swing.text.GapContent;
- public class Main {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int n, m;
- int[][] dp = new int[1010][1010];
- String cmd = new String();
- for(int i = 0; i < 1010; i++) {
- for(int j = 0; j < 1010; j++) {
- dp[i][j] = 0;
- }
- }
- n = in.nextInt(); m = in.nextInt();
- int x1, x2, y1, y2, num;
- while(m-- > 0) {
- cmd = in.next();
- if(cmd.charAt(0) == 'A') {
- x1 = in.nextInt();
- y1 = in.nextInt();
- num = in.nextInt();
- for(int i = y1; i < n; i++) {
- dp[x1][i] = (dp[x1][i] + num) % 1000000007;
- }
- }
- else {
- x1 = in.nextInt();
- x2 = in.nextInt();
- y1 = in.nextInt();
- y2 = in.nextInt();
- int ret = 0;
- for(int i = x1; i <= y1; i++) {
- if(x2 == 0) ret = (ret + dp[i][y2]) % 1000000007;
- else ret = (ret + dp[i][y2] - dp[i][x2-1]) % 1000000007;
- }
- System.out.println((ret + 1000000007) % 1000000007);
- }
- }
- }
- }
C
[HIHO]hihoCoder太阁最新面经算法竞赛7的更多相关文章
- Hihocoder 太阁最新面经算法竞赛18
Hihocoder 太阁最新面经算法竞赛18 source: https://hihocoder.com/contest/hihointerview27/problems 题目1 : Big Plus ...
- hihoCoder太阁最新面经算法竞赛15
hihoCoder太阁最新面经算法竞赛15 Link: http://hihocoder.com/contest/hihointerview24 题目1 : Boarding Passes 时间限制: ...
- hihocoder Round #c1(hihoCoder太阁最新面经算法竞赛1 )
Test链接:https://cn.vjudge.net/contest/231849 选自hihoCoder太阁最新面经算法竞赛1 更多Test:传送门 A:区间求差 给一组区间集合A和区间集合B, ...
- hihoCoder太阁最新面经算法竞赛19
比赛链接:http://hihocoder.com/contest/hihointerview28/problems A. 固定一个方向,两两相邻的点顺时针或逆时针构造三个向量,判断这个点在这个向量的 ...
- hihoCoder太阁最新面经算法竞赛18
比赛链接:http://hihocoder.com/contest/hihointerview27/problems A.Big Plus 模拟水 #include <bits/stdc++.h ...
- hihoCoder太阁最新面经算法竞赛17
比赛链接:http://hihocoder.com/contest/hihointerview26 A.排序后枚举两个点,确定一个矩形后二分剩下两个点. #include <bits/stdc+ ...
- zz 圣诞丨太阁所有的免费算法视频资料整理
首发于 太阁实验室 关注专栏 写文章 圣诞丨太阁所有的免费算法视频资料整理 Ray Cao· 12 小时前 感谢大家一年以来对太阁实验室的支持,我们特地整理了在过去一年中我们所有的原创算法 ...
- [刷题]算法竞赛入门经典 3-12/UVa11809
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa11809:Floating-Point Numbers 代码: //UVa11 ...
- [刷题]算法竞赛入门经典 3-1/UVa1585 3-2/UVa1586 3-3/UVa1225
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO(我也是在网上找到的pdf,但不记得是从哪里搜刮到的了,就重新上传了一遍) PS:第一次写博客分享我的代码,不知道我对c ...
随机推荐
- Word2007插入两种页码
做毕设,摘要,Abstract,目录,第一章,现在想要“摘要,Abstract,目录”编页码“为罗马数字,第一章开始为阿拉伯数字,可以按如下步骤: 1.各部分插入分页符,这与插入两种页码无关,不过是为 ...
- java 验证身份证号
- CSS3 3D变换
可以这么说,3D变换是基于三维坐标系的.以下是“盗用”的图 CSS3中的3D变换主要包括以下几个功能函数: 3D位移:包括translateZ()和translate3d(): 3D旋转:包括rota ...
- PE文件结构深入详解
一.PE结构基础 看了很多PE结构类的东东,要不上来就是整体结构,要不就是一大堆ASM代码,看的我等菜鸟有点难受!所以自己写个帖·学习PE我们先来弄懂几个问题! 1:几个地址的概念 VA:虚拟地址,也 ...
- linux 访问ntfs分区
打开ntfs-3g的下载点http://www.tuxera.com/community/ntfs-3g-download/ ,将最新稳定(当前最新版本为ntfs-3g-2011.1.15)下载到Ce ...
- 对drupal的理解【转】
写本文是想跟刚用drupal的朋友,分享一下心得,国内用drupal的太少了,希望大家能好好交流. 希望几分钟看完后你能马上上手drupal,至少能理解hook,api,theme,module,cc ...
- 单例模式及C++实现
单例模式及C++实现代码 C++中的单例模式http://blog.csdn.net/hackbuteer1/article/details/7460019
- java cookie
public static void AddCookie(HttpServletResponse response, String key, String value) { Cookie cookie ...
- 由CAST()函数在.NET1.1和.NET4.0下处理机制不同所引发的BUG
.NET 1.1版本下使用日期强制转换函数,比如:"select cast(ActionDate as char(7)) as ActionDate from ST_BookAction ...
- 关于CStdioFile的使用问题
在win32控制台调试如下程序 #include "stdafx.h"#include <afx.h>//#include <iostream>//usin ...