[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

上一篇:Android第三方应用分享图文到微信朋友圈 & 微信回调通知分享状态


下一篇:js导出execl兼容ie Chrome Firefox各种主流浏览器(js export execl)