微软2014实习生及秋令营技术类职位在线测试

题目1 : String reorder

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

Description

For this question, your program is required to process an input string containing only ASCII characters between ‘0’ and ‘9’, or between ‘a’ and ‘z’ (including ‘0’, ‘9’, ‘a’, ‘z’).

Your program should reorder and split all input string characters into multiple segments, and output all segments as one concatenated string. The following requirements should also be met,
1. Characters in each segment should be in strictly increasing order. For ordering, ‘9’ is larger than ‘0’, ‘a’ is larger than ‘9’, and ‘z’ is larger than ‘a’ (basically following ASCII character order).
2. Characters in the second segment must be the same as or a subset of the first segment; and every following segment must be the same as or a subset of its previous segment.

Your program should output string “<invalid input string>” when the input contains any invalid characters (i.e., outside the '0'-'9' and 'a'-'z' range).

 

Input


Input consists of multiple cases, one case per line. Each case is one string consisting of ASCII characters.

Output


For each case, print exactly one line with the reordered string based on the criteria above.

 

样例输入
aabbccdd
007799aabbccddeeff113355zz
1234.89898
abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee
样例输出
abcdabcd
013579abcdefz013579abcdefz
<invalid input string>
abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa

 C++ Code 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
 
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <map>

using namespace std;

bool isInvalid(char c)
{
    if(c >= '0' && c <= '9')
    {
        return true;
    }
    else if(c >= 'a' && c <= 'z')
    {
        return true;
    }

    return false;
}

int main()
{
    freopen("in.txt""r", stdin);
    map<charint> myMap;
    string str;
    char tmp;
    while(getline(cin, str))
    {
        map<charint>().swap(myMap);

        bool flag = true;
        for(int i = 0; i < str.size(); ++i)
        {
            if(!isInvalid(str[i]))
            {
                printf("<invalid input string> ");
                flag = false;
                break;
            }

            if(myMap.find(str[i]) != myMap.end())
            {
                myMap[str[i]]++;
            }
            else
            {
                myMap[str[i]] = 1;
            }
        }
        if(!flag)
        {
            continue;
        }
        for(int n = 0; n < str.size();)
        {
            for(int i = 0; i <= 9; ++i)
            {
                tmp = i + '0';
                if(myMap.find(tmp) != myMap.end() && myMap[tmp] > 0)
                {
                    printf("%c", tmp);
                    myMap[tmp]--;
                    ++n;
                }
            }
            for(int i = 0; i <= 26; ++i)
            {
                char tmp = i + 'a';
                if(myMap.find(tmp) != myMap.end() && myMap[tmp] > 0)
                {
                    printf("%c", tmp);
                    myMap[tmp]--;
                    ++n;
                }
            }
        }
        printf(" ");
    }

    return 0;
}


题目2 : K-th string

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

Description

Consider a string set that each of them consists of {0, 1} only. All strings in the set have the same number of 0s and 1s. Write a program to find and output the K-th string according to the dictionary order. If such a string doesn’t exist, or the input is not valid, please output “Impossible”. For example, if we have two ‘0’s and two ‘1’s, we will have a set with 6 different strings, {0011, 0101, 0110, 1001, 1010, 1100}, and the 4th string is 1001.

Input

The first line of the input file contains a single integer t (1 ≤ t ≤ 10000), the number of test cases, followed by the input data for each test case.
Each test case is 3 integers separated by blank space: N, M(2 <= N + M <= 33 and N , M >= 0), K(1 <= K <= 1000000000). N stands for the number of ‘0’s, M stands for the number of ‘1’s, and K stands for the K-th of string in the set that needs to be printed as output.

Output

For each case, print exactly one line. If the string exists, please print it, otherwise print “Impossible”.

 

样例输入
3
2 2 2
2 2 7
4 7 47
样例输出
0101
Impossible
01010111011
 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
 
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>


using namespace  std;

int main()
{
    freopen("in.txt""r", stdin);
    int t, N, M, K;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d %d %d", &N, &M, &K);
        vector<int> vec;
        for(int i = 0; i < N; ++i)
        {
            vec.push_back(0);
        }
        for (int j = 0; j < M; ++j)
        {
            vec.push_back(1);
        }
        int count = 0;
        bool flag = false;
        do
        {
            count++;
            if (count == K)
            {
                flag = true;
                break;
            }
        }
        while (next_permutation(vec.begin(), vec.end()));

        if (flag == true)
        {
            for (int i = 0; i < M + N; ++i)
            {
                printf("%d", vec[i]);
            }
            printf(" ");
        }
        else
        {
            printf("Impossible ");
        }

    }

    return 0;
}

题目3 : Reduce inversion count

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

Description

Find a pair in an integer array that swapping them would maximally decrease the inversion count of the array. If such a pair exists, return the new inversion count; otherwise returns the original inversion count.

Definition of Inversion: Let (A[0], A[1] ... A[n], n <= 50) be a sequence of n numbers. If i < j and A[i] > A[j], then the pair (i, j) is called inversion of A.

Example:
Count(Inversion({3, 1, 2})) = Count({3, 1}, {3, 2}) = 2
InversionCountOfSwap({3, 1, 2})=>
{
 InversionCount({1, 3, 2}) = 1 <-- swapping 1 with 3, decreases inversion count by 1
 InversionCount({2, 1, 3}) = 1 <-- swapping 2 with 3, decreases inversion count by 1
 InversionCount({3, 2, 1}) = 3 <-- swapping 1 with 2 , increases inversion count by 1
}

 

Input

Input consists of multiple cases, one case per line.Each case consists of a sequence of integers separated by comma. 

Output

For each case, print exactly one line with the new inversion count or the original inversion count if it cannot be reduced.

 

样例输入
3,1,2
1,2,3,4,5
样例输出
1
0
 
 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
 
#include<cstdio>
#include<cstdlib>
#include <iostream>
#include <string>

using namespace std;

int merge(int arr[], int temp[], int left, int mid, int right);
int _mergeSort(int arr[], int temp[], int left, int right);

int mergeSort(int arr[], int array_size)
{
    int *temp = (int *)malloc(sizeof(int) * array_size);
    return _mergeSort(arr, temp, 0, array_size - 1);
}

int _mergeSort(int arr[], int temp[], int left, int right)
{
    int mid, inv_count = 0;
    if(right > left)
    {
        mid = (right + left) / 2;
        inv_count = _mergeSort(arr, temp, left, mid);
        inv_count += _mergeSort(arr, temp, mid + 1, right);
        inv_count += merge(arr, temp, left, mid + 1, right);
    }
    return inv_count;
}

int merge(int arr[], int temp[], int left, int mid, int right)
{
    int i, j, k;
    int inv_count = 0;
    i = left;
    j = mid;
    k = left;
    while((i <= mid - 1) && (j <= right))
    {
        if(arr[i] <= arr[j])
            temp[k++] = arr[i++];
        else
        {
            temp[k++] = arr[j++];
            inv_count = inv_count + (mid - i);
        }
    }
    while(i <= mid - 1)
        temp[k++] = arr[i++];
    while(j <= right)
        temp[k++] = arr[j++];
    for(i = left; i <= right; i++)
        arr[i] = temp[i];

    return inv_count;
}

int a[100000];


int main()
{
    freopen("in.txt""r", stdin);
    string str;
    int old, tmp, newInversion, diff, minDiff;
    int count;
    while(getline(cin, str))
    {
        count = 0;
        for(int i = 0; i < str.size(); ++i)
        {
            if (str[i] != ',' && str[i] != ' ')
            {
                a[count] = str[i] - '0';
                count++;
                if (count == 50)
                {
                    break;
                }
            }
        }

        //先求原先的逆序数,遍历交互和这个做比较,
        old = mergeSort(a, count);
        diff = 0;
        minDiff = diff;
        for (int i = 0; i < count; ++i)
        {
            for (int j = 0; j < count; ++j)
            {
                if (i != j)
                {
                    tmp = a[i];
                    a[i] = a[j];
                    a[j] = tmp;
                    diff = old - mergeSort(a, count);
                    if (diff > 0)
                    {
                        if (diff > minDiff)
                        {
                            minDiff = diff;
                        }
                    }
                    tmp = a[i];
                    a[i] = a[j];
                    a[j] = tmp;
                }
            }
        }
        printf("%d ", old - minDiff);
    }
}

//int main()
//{
//  freopen("in.txt", "r", stdin);
//  int n;
//  scanf("%d",&n);
//  while(n>0)
//  {
//      n--;
//      int m;
//      scanf("%d", &m);
//      for(int i=0;i<m;i++)
//          scanf("%d", &a[i]);
//
//      printf("%d ",mergeSort(a, m));
//  }
//}
这个过了40%的数据。因为没有考虑两位数字的情况,多位数字的情况。坑爹啊,这个太傻比了……
 
同学用java过了:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.TreeMap;


public class Main
{
    public static void main(String[] args)
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        ArrayList<String> inputs = new ArrayList<String>();
        String line = null;
        try
        {
            while ((line = br.readLine()) != null)
            {
                inputs.add(line);
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
for (String s : inputs)
        {
            String[] arg = s.split(",");
            if(arg.length > 50)
                continue;
            int[] a = new int[arg.length];
            for (int i = 0; i < arg.length; i++)
            {
                a[i] = Integer.parseInt(arg[i].trim());
            }
            System.out.println(count(a));
        }
    }

    private static int count(int[] a)
    {
        int min = countCore(a);
        for (int i = 0; i < a.length - 1; i++)
            for (int j = i + 1; j < a.length; j++)
            {
                min = Math.min(min, countCore(exchange(a, i, j)));
            }
        return min;
    }

    private static int[] exchange(int[] a, int i, int j)
    {
        int[] b = new int[a.length];
        for (int k = 0; k < a.length; k++)
        {
            b[k] = a[k];
        }
        int tmp = b[i];
        b[i] = b[j];
        b[j] = tmp;
        return b;
    }


    private static int countCore(int[] a)
    {

        int inversion = 0;
        for (int i = 0; i < a.length - 1; i++)
        {
            for (int j = i + 1; j < a.length; j++)
            {
                if (a[j] < a[i])
                    inversion++;
            }
        }
        return inversion;
    }
}

题目4 : Most Frequent Logs

时间限制:10000ms
单点时限:3000ms
内存限制:256MB

Description

In a running system, there are many logs produced within a short period of time, we'd like to know the count of the most frequent logs.

Logs are produced by a few non-empty format strings, the number of logs is N(1<=N<=20000), the maximum length of each log is 256.

Here we consider a log same with another when their edit distance (see note) is <= 5.

Also we have a) logs are all the same with each other produced by a certain format string b) format strings have edit distance  5 of each other.

Your program will be dealing with lots of logs, so please try to keep the time cost close to O(nl), where n is the number of logs, and l is the average log length.

Note edit distance is the minimum number of operations (insertdeletereplace a character) required to transform one string into the other, please refer to

 

http://en.wikipedia.org/wiki/Edit_distance for more details.

 

 

Input

Multiple lines of non-empty strings.

Output


The count of the most frequent logs.

 

样例输入
Logging started for id:1
Module ABC has completed its job
Module XYZ has completed its job
Logging started for id:10
Module ? has completed its job
样例输出
3
 
 
这道题目没有a出来。
原文地址:https://www.cnblogs.com/codemylife/p/3661380.html