2017 雷火研发笔试3.20

总共4个编程题,分别是10,20,30,40分。

 

水题AC代码:

 1 #include "iostream"
 2 #include "string.h"
 3 #include "vector"
 4 
 5 using namespace std;
 6 
 7 int n, m;
 8 
 9 int v[10005];
10 
11 void turn(int l, int r)
12 {
13     for (int i = l; i <= r; i++)
14     {
15         v[i] = !v[i];
16     }
17 }
18 
19 int main()
20 {
21     while (cin >> n >> m)
22     {
23         memset(v, 0, 10005);
24         int a, b;
25         for (int i = 0; i < m; i++)
26         {
27             cin >> a >> b;
28             turn(a, b);
29         }
30         
31         for (int i = 1; i <= n; i++)
32             cout << v[i];
33 
34         cout << endl;
35     }
36 }
37 
38 //5 2
39 //1 2
40 //2 4

 

 

第二题:

 

 

第三题:

并查集,秒A

 1 #include "iostream"
 2 #include "string.h"
 3 using namespace std;
 4 
 5 int  pre[105];
 6 bool t[105];               //t 用于标记独立块的根结点
 7 
 8 int Find(int x)
 9 {
10     int r = x;
11     while (r != pre[r])
12         r = pre[r];
13 
14     int i = x, j;
15     while (pre[i] != r)
16     {
17         j = pre[i];
18         pre[i] = r;
19         i = j;
20     }
21     return r;
22 }
23 
24 void mix(int x, int y)
25 {
26     int fx = Find(x), fy = Find(y);
27     if (fx != fy)
28     {
29         pre[fy] = fx;
30     }
31 }
32 
33 int main()
34 {
35     int N, M, a, b, i, j, ans;
36     while (cin>>N>>M)
37     {
38         for (i = 1; i <= N; i++)          //初始化 
39             pre[i] = i;
40 
41         for (i = 1; i <= M; i++)          //吸收并整理数据 
42         {
43             cin >> a >> b;
44             mix(a, b);
45         }
46 
47         memset(t, 0, sizeof(t));
48         for (i = 1; i <= N; i++)          //标记根结点
49         {
50             t[Find(i)] = 1;
51         }
52         for (ans = 0, i = 1; i <= N; i++)
53             if (t[i])
54                 ans++;
55 
56         cout<<ans - 1<<endl;
57 
58     }
59     return 0;
60 }

 

第四题:

规约成构造回文串的题,A了81%

#include "iostream"
#include "string"
#include "vector"
#include "algorithm"
#include "set"
using namespace std;

int tt = 0;
int helper(string s)
{
    string rev = s;
    reverse(rev.begin(), rev.end());

    if (rev == s)    return 0;

    int size = s.size();
    vector<vector<int>> c(size + 1, vector<int>(size + 1));
    for (int i = 1; i <= size; ++i)
        for (int j = 1; j <= size; ++j)
        {
            if (s[i - 1] == rev[j - 1])
            {
                c[i][j] = c[i - 1][j - 1] + 1;
            }
            else if (c[i - 1][j] >= c[i][j - 1])
            {
                c[i][j] = c[i - 1][j];
            }
            else if (c[i - 1][j] < c[i][j - 1])
            {
                c[i][j] = c[i][j - 1];
            }
        }
    return size - c[size][size];
}
int main()
{
    string s;
    while (cin >> s)
    {
        set<char> se;
        for (int i = 0; i < s.length(); i++)
        {
            se.insert(s[i]);
        }
        tt = se.size();
        if (helper(s) == 0)
            cout << tt << endl;
        else
            cout << helper(s) +tt -1 << endl;
    }
        
}

//0.81

 

posted @ 2017-03-21 09:23  SeeKHit  阅读(565)  评论(0编辑  收藏  举报