2014/04/12微軟實習生在線測試題

昨天參加微軟2014實習生及秋令營技術類職位在線測試,今天騰訊面試,相當鬱悶,還是踏實看書吧,下面提供自己寫的一份參考程序,用的c++。

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
#include <map>
#include <iostream>
#include <string>
using namespace std;
void main(){
	string str;
	cin>>str;
	map<char,int> word_count;
	for(auto i=str.cbegin();i!=str.end();++i){
		if((*i>='0'&&*i<='9')||(*i>='a'&&*i<='z'))
			++word_count[*i];
		else {
			cout<<"<invalid input string>"<<endl;
			return;
		}
	}
	while(word_count.cbegin()!=word_count.cend()){
		for(auto j=word_count.begin();j!=word_count.end();){
			cout<<j->first;
			--word_count[j->first];
			if(word_count[j->first]==0)
			{
				j=word_count.erase(j);
			}
			else
				++j;
		}
	}
	cout<<endl;
}

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

#include <iostream>//好像超時了,僅供參考。
#include <vector>
#include <set>
#include <string>
using namespace std;
void Permutation(string &pStr, int pBegin,set<string>&setofstring)//遞歸生成01的set
{
	if(pBegin == pStr.size())
    {
		setofstring.insert(pStr);
    }
    else
    {
		for(int pCh = pBegin; pCh != pStr.size(); ++ pCh)
        {
            swap(pStr[pCh],pStr[pBegin]);
            Permutation(pStr, pBegin + 1,setofstring);
			swap(pStr[pCh],pStr[pBegin]);
        }
    }
}
void findelem(set<string> setofstr,unsigned int k,string &str){//找出第k個元素放到vector中
	auto j=setofstr.begin();
	for(int i=1;i<k;i++){
		j++;
		if(j==setofstr.cend()){
			str.assign("Invalid input");
			return;
		}
	}
	str=*j;
}
void K_th(vector<string>&vecofstr){
	unsigned int t;
	cin>>t;
	string str;
	string temp;
	set<string> setofstr;
	unsigned int N,M,k,count=0;
	for(unsigned int h=0;h!=t;h++){
		cin>>N>>M>>k;
		count=0;
		str.clear();
		for(unsigned int i=0;i!=N;i++)
			str.insert(str.size(),1,'0');
		for(unsigned int j=0;j!=M;j++)
			str.insert(str.size(),1,'1');
		Permutation(str,0,setofstr);
		findelem(setofstr,k,temp);
		vecofstr.push_back(temp);
	}
}
int main()
{
	vector<string> vecofstr;
	K_th(vecofstr);
	for(auto g=vecofstr.cbegin();g!=vecofstr.cend();++g)
		cout<<*g<<endl;
    return 0;
} 

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

#include <iostream>//這個輸入輸出格式沒注意,好像還得有逗號,這裏默認序列測試的。
using namespace std;
int InversePairsCore(int *data,int *copy,int start,int end){//計算逆序數
	if(start==end){
		copy[start]=data[start];
		return 0;
	}
	int length=(end-start)/2;
	int left=InversePairsCore(copy,data,start,start+length);
	int right=InversePairsCore(copy,data,start+length+1,end);
	int i=start+length;
	int j=end;
	int indexCopy=end;
	int count=0;
	while(i>=start && j>=start+length+1){
		if(data[i]>data[j]){
			copy[indexCopy--]=data[i--];
			count +=j-start-length;
		}
		else{
			copy[indexCopy--]=data[j--];
		}
	}
	for(;i>=start;--i)
		copy[indexCopy--]=data[i];
	for(;j>=start+length+1;--j)
		copy[indexCopy--]=data[j];
	return left+right+count;
}
int inversepairs(int *data,int length){
	if(data==nullptr || length<0)
		return 0;
	int *copy=new int[length];
	for(int i=0;i!=length;++i){
		copy[i]=data[i];
	}
	int count=InversePairsCore(data,copy,0,length-1);
	delete[] copy;
	return count;
}
int inversecount(int *data,int length){//窮舉得結果
	int count=inversepairs(data,length);
	int temp=0;
	for(int i=0;i!=length;++i){
		for(int j=i+1;j!=length;++j){
			swap(data[i],data[j]);
			temp=inversepairs(data,length);
			if(temp<count)count=temp;
			swap(data[i],data[j]);
		}
	}
	return count;
}
void main(){
	int data[]={2,3,1,5};
	cout<<inversecount(data,4)<<endl;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章