微软10.10日笔试前三题

ms实习生,投了简历,,竟然让笔试了。面试机会估计不会有的,,所以大家放心,不和你们抢饭碗

1.

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n, a, b, x;
    while(scanf("%d", &n) != EOF) {
        a = 0;
        b = 0;
        for (int i = 0; i < n; i++) {
            scanf("%d", &x);
            if (x & 1) ++a; else ++b;
        }
        printf("%d\n", n - (min(a, b) << 1));
    }
    return 0;
} 

2.

#include <bits/stdc++.h>
using namespace std;

char a[100010];
char b[210][2];
int dp[100010][2][26];

int n, m;
bool conflict(char aa, char bb) {
    for (int i = 0; i < m; i++) {
        if (b[i][0] == aa && b[i][1] == bb) return true;
        if (b[i][0] == bb && b[i][1] == aa) return true;
    }
    return false;
}

int main()
{
    while(scanf("%d", &n) != EOF) {
        memset(dp, 0, sizeof(dp));
        getchar();
        for (int i = 1; i <= n; i++) {
            scanf("%c", &a[i]);
        }
        scanf("%d", &m);
        for (int i = 0; i < m; i++) {
            getchar();
            scanf("%c%c", &b[i][0], &b[i][1]);
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j < 26; j++) {
                dp[i][0][j] = dp[i-1][0][j];
                if (!conflict(a[i], j + 'a')) {
                    dp[i][1][a[i] - 'a'] = max(dp[i][1][a[i] - 'a'], dp[i-1][0][j] + 1);
                }
            }
            if (i != 1) {
                dp[i][0][a[i-1] - 'a'] = max(dp[i][0][a[i-1] - 'a'], dp[i-1][1][a[i-1] - 'a']);
            }
            if (i != 1 && !conflict(a[i], a[i-1])) {
                dp[i][1][a[i] - 'a'] = max(dp[i][1][a[i] - 'a'], dp[i-1][1][a[i-1] - 'a'] + 1);
            }
        }
        int ans = 0;
        for (int i = 0; i < 26; i++) {
            ans = max(ans, dp[n][0][i]);
        }
        ans = max(ans, dp[n][1][a[n] - 'a']);
        printf("%d\n", n - ans);
    }
    return 0;
}

3.

 #include <bits/stdc++.h>
using namespace std;

const int N = 10010;
const int M = 110;
int n, m, k;
struct Node {
    int time;
    int sn;
    int pw;
    int index;
    bool operator < (const Node &node) const {
        if (time > node.time) return true;
        if (time < node.time) return false;
        return (sn > node.sn);
    }
    Node() {}
    Node(int a1, int a2, int a3, int a4): time(a1), sn(a2), pw(a3), index(a4) {}
};
int s[N], t[N], p[N];
int o[N][M];
int w[N][M];
int ans[N];
int earliest[M];

int main()
{
    while(scanf("%d%d%d", &n, &m, &k) != EOF) {
        priority_queue<Node> Q;
        for (int i = 0; i < n; i++) {
            scanf("%d%d%d", &s[i], &t[i], &p[i]);
            for (int j = 0; j < p[i]; j++) {
                scanf("%d%d", &o[i][j], &w[i][j]);
            }
            Q.push(Node(t[i] + k, s[i], -1, i));
        }
        for (int i = 0; i < m; i++) {
            earliest[i] = 0;
        }
        while(!Q.empty()) {
            Node now = Q.top();
            Q.pop();
            if (now.pw == p[now.index] - 1) {
                ans[now.index] = now.time - k;
                continue;
            }
            now.pw++;
            now.time = max(now.time, earliest[o[now.index][now.pw]]) + w[now.index][now.pw] + k;
            earliest[o[now.index][now.pw]] = now.time - k;
            Q.push(now);
        }
        for (int i = 0; i < n; i++) {
            printf("%d\n", ans[i]);
        }
    }
    return 0;
} 
4. 被block住了挺长时间,发挥骗分导论精神,10分滚粗。

前三道AC+10分
全部评论
大神不要虐我们啊
点赞 回复 分享
发布于 2016-10-10 23:28
这都没面试机会几个人会有
点赞 回复 分享
发布于 2016-10-10 23:31
夜晚也参加了一下,主要是想感受一下别人是多牛逼!大神,请收下我的膝盖!
点赞 回复 分享
发布于 2016-10-10 23:32
大神,能稍微解释一下二三题吗
点赞 回复 分享
发布于 2016-10-10 23:32
***
点赞 回复 分享
发布于 2016-10-10 23:32
大神,您google的offer拿到了么
点赞 回复 分享
发布于 2016-10-10 23:40
可以解释一下第二题吗?
点赞 回复 分享
发布于 2016-10-10 23:59
一般过几题有面试机会啊?。。看了下过两题有800多。。3题的是200多。。
点赞 回复 分享
发布于 2016-10-11 00:04
这个第二题的dp方程的第二维在干什么……完全没看懂…… 我直接dp[i][j]表示0..i的子串中,最后一个字母是j的合法子序列最长多长…… 转移2部分讨论: A:不保留这个i字母,那和i-1一样,抄一遍 B:保留这个i字母,考虑和i-1的情况下所有合法可能接起来。
点赞 回复 分享
发布于 2016-10-11 00:13
大神,最后一题,,如何骗分呀?直接输入输出一个固定的数字?
点赞 回复 分享
发布于 2016-10-11 10:41
点赞 回复 分享
发布于 2016-10-11 11:09
/* 我对题目2的思路: 根据每个字母的约束条件,找到满足条件的最长子串,答案就是长度减去这个最长子串的长度。 */ #include <cstdio> #include <string.h> #include <algorithm> using namespace std; const int MAX = 100010; char str[MAX]; int invalid[30][30], dp[26]; int main() { int n, m; while (scanf("%d", &n) != EOF){ scanf("%s", str); scanf("%d", &m); memset(invalid, 0, sizeof(invalid)); memset(dp, 0, sizeof(dp)); for (int i = 0; i<m; i++){ char ch1, ch2; getchar(); scanf("%c%c", &ch1, &ch2); invalid[ch1 - 'a'][ch2 - 'a'] = invalid[ch2 - 'a'][ch1 - 'a'] = 1; } for (int i = 0; i<n; i++){ char a = str[i]; int tmp = 1; for (int j = 0; j<26; j++){ if (invalid[a - 'a'][j]) continue; tmp = max(tmp, dp[j] + 1); } //printf("%d ", tmp); dp[a - 'a'] = tmp; } sort(dp, dp + 26); printf("%d\n", n - dp[25]); } } /* 我对题目3的思路: 用队列模拟注册流程,需要判断到达时间,决定办理先后顺序 */ #include <iostream> #include <vector> #include <queue> using namespace std; const int N = 10010; const int INF = 0x3f3f3f3f; int n, m, k; struct node { int now; //arrive time Ti int id; //student number Si; int index; int len; //visit number of Pi offices int v; //index vector<int> s; //Oij offices vector<int> t; //Wij processing time node(){ now = id = index = len = v = 0; }; }; node *pNode[N]; int res[N], beg[105]; struct mycmp { bool operator()(node *a, node *b){ if (a->now == b->now) return a->id > b->id; return a->now>b->now; } }; priority_queue<node*, vector<node*>, mycmp> q; int main() { scanf("%d %d %d", &n, &m, &k); for (int i = 0; i<n; i++){ int num; //num is Pi offices pNode[i] = new node(); scanf("%d %d %d", &pNode[i]->id, &pNode[i]->now, &num); pNode[i]->v = i; pNode[i]->len = num; int a, b; //Oij and Wij for (int j = 0; j<num; j++){ scanf("%d %d", &a, &b); pNode[i]->s.push_back(a); pNode[i]->t.push_back(b); } pNode[i]->index = 0;//start from first office pNode[i]->now += k;//from gate to first office q.push(pNode[i]); } while (!q.empty()){ node *tmp = q.top(); q.pop(); int b = max(tmp->now, beg[tmp->s[tmp->index]]); tmp->now = beg[tmp->s[tmp->index]] = b + tmp->t[tmp->index]; if (tmp->index == tmp->len - 1){ //end of registration res[tmp->v] = tmp->now; } else{ tmp->now += k;//from one office to another tmp->index++; q.push(tmp); } } for (int i = 0; i<n; i++) printf("%d\n", res[i]); return 0; }
点赞 回复 分享
发布于 2016-10-11 15:37

相关推荐

球球别再泡了:坏,我单9要了14
点赞 评论 收藏
分享
评论
点赞
26
分享
牛客网
牛客企业服务