微软2014实习生及秋令营技术类职位在线测试[题目与答案]

微软2014实习生及秋令营技术类职位在线测试[题目与答案]

更多算法、笔试题学习请关注我的博客  编程者博客  http://www.coderblog.cn/

题目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#
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
using System;
 
 
    class Program
    {
        static bool IsValid(string str)
        {
            bool result = true;
            for (int i = 0; i < str.Length; i++)
            {
                if ((str[i] >= '0' && str[i] <= '9') || (str[i] >= 'a' && str[i] <= 'z'))
                {
                }
                else
                {
                    result = false;
                    break;
                }
            }
            return result;
        }
        static string StringRecorder(string str)
        {
 
            if (IsValid(str))
            {
                int[] bitmap = new int[36];
                char[] strbuf = new char[str.Length];
                int index = 0;
                int i;
                for (i = 0; i < 36; i++)
                {
                    bitmap[i] = 0;
                }
                for (i = 0; i < str.Length; i++)
                {
                    char c = str[i];
                    if (c >= '0' && c <= '9')
                    {
                        bitmap[(int)(c - '0')] += 1;
                    }
                    else
                    {
                        bitmap[(int)(c - 'a'+10)] += 1;
                    }
                }
                 
                while (index < str.Length)
                {
                    for (i = 0; i < 36; i++)
                    {
                        if (bitmap[i] > 0)
                        {
                            bitmap[i] -= 1;
                            strbuf[index] = i <= 9 ? (char)('0' + i) :(char)( 'a' + i - 10);
                            index += 1;
                        }
                    }
                }
                return new String(strbuf);
            }
            else
            {
                return "<invalid input string>";
            }
        }
        static void Main(string[] args)
        {
            string line;
            while ((line = Console.ReadLine()) != null)
            {
                Console.WriteLine(StringRecorder(line));
            }
        }
    }

  

题目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语言
复制代码
#include <stdio.h>
#include <memory.h>

#define S 100
int n1,n0,kCount,answer[100];
int kth=0;
int k=0;
int found = 0;
void Dfs(int m,int n,int offset)
{
   int  i;
   int j;
   if(found == 1)return;
   if(m==0)
   {
        for(i=n0+n1;i>n0+n1-n;i--)
            answer[i] = 1;
        k++;
        if(k==kth)
        {
            found = 1;
            for(i=1;i<=n1+n0;i++)
                printf("%d",answer[i]);
            printf("
");
        }
    
   }
   else if(n==0)
   {     
        for(i=n0+n1;i>n0+n1-m;i--)
            answer[i] = 0;
        k++;
        if(k==kth)
        {
            found = 1;
            for(i=1;i<=n1+n0;i++)
                printf("%d",answer[i]);
            printf("
");
        }
   }
   else 
   { 
     for(i=m;i>=0;i--)
     {
         for(j=i;j>0;j--)
            answer[offset+j] = 0;
         answer[offset+i+1] = 1;
         Dfs(m-i,n-1,offset+i+1);
     }
   }
}
int main() 
{ 
  int t;
  int i;
  
  scanf("%d",&t);
  for(i=0;i<t;i++)
  {
    scanf("%d %d %d",&n1,&n0,&kth);
      memset(answer,0,sizeof(answer));          
      found=0;
      k=0;
    Dfs(n1,n0,0);          
    if(found==0)
    {
        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

复制代码
using System;


    class Program
    {
        static int mergeSort(int[] arr, int array_size)
        {
            int[] temp = new int[array_size];
            return _mergeSort(arr, temp, 0, array_size - 1);
        }
 
        static 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;
        }

        static 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;
        }
        static void Swap(int[] arr, int i, int j)
        {
            int t = arr[i];
            arr[i] = arr[j];
            arr[j] = t;
        }
        static void Copy(int[] arr, int n, int[] dst)
        {
            int i;
            for (i = 0; i < n; i++)
            {
                dst[i] = arr[i];
            }
        }
        static int ReduceInvCount(int[] arr, int n)
        {
            int i, j;
            int min = 0;
            int count = 0;
            int[] buf = new int[n];
            
            Copy(arr, n, buf);
            min = mergeSort(buf, n);
            for (i = 0; i < n - 1; i++)
            {
                for (j = i + 1; j < n; j++)
                {
                    Swap(arr, i, j);
                    Copy(arr, n, buf);
                    count = mergeSort(buf, n);
                    Swap(arr, i, j);
                    if (count < min) min = count;
                }
            }
            
            return min;
        }
        static void Main(string[] args)
        {
            string line;
            while ((line = Console.ReadLine()) != null)
            {
                if (line == "")
                {
                    break;
                }
                else
                {
                    string[] token = line.Split(',');
                    int[] arr = new int[token.Length];
                    for (int i = 0; i < token.Length; i++)
                    {
                        arr[i] = int.Parse(token[i]);
                    }
                    Console.WriteLine(ReduceInvCount(arr, arr.Length));
                }
            }
        }
    }
复制代码

题目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

答案:没做出来T_T

得分 题目 结果 语言 时间 内存 提交时间 操作
50 / 100 Most Frequent Logs TLE C# 20865ms 21MB 1小时前 查看
50 / 100 Most Frequent Logs WA C# 274ms 15MB 1小时前 查看
100 / 100 Reduce inversion count AC C# 566ms 30MB 2小时前 查看
100 / 100 K-th string AC GCC 26ms 15MB 2小时前 查看
70 / 100 K-th string PE GCC 14ms 48MB 2小时前 查看
/ 100 K-th string CE GCC 0ms 0MB 2小时前 查看
/ 100 K-th string CE GCC 0ms 0MB 2小时前 查看
100 / 100 String reorder AC C# 457ms 11MB 3小时前 查看
/ 100 String reorder CE GCC 0ms 0MB 3小时前 查看
/ 100 String reorder CE GCC 0ms 0MB 3小时前 查看
原文地址:https://www.cnblogs.com/Leo_wl/p/3661568.html