微軟2014實習生及秋令營技術類職位在線測試第一題:String reorder

如題,微軟很重視算法的考察,同時也重視算法程序的實現,本題是筆者參加微軟暑期實習的前期在線測試的一道編程題,得分是100,現與大家分享一下當時的實現算法!


本題翻譯過來簡述如下:輸入的字符串只包括‘0’-‘9’和‘a’-‘z’ 之間ASCII字符,要求算法實現字符記錄分割成多個子段,需滿足1. 字符在每個子段依照ASCII碼值屬於嚴格遞增序列,2. 後一個子段必須屬於前一個子段的子集,即相同或者是真子集,3. 當輸入包含不符合輸入條件的字符時,輸出 <invalid input string>


算法核心思想:取兩個數組Num[10] 和 Char[26]分別記錄合法輸入字符的個數,這裏利用一個經典的思想,即如果字符串中一個字符char c= ‘9’,則Num[c-'0']++ 即爲其個數(數值本身可轉化爲下標值的經典思想,取材於霍夫曼字符壓縮編碼思想),這樣遍歷一遍輸入字符串後即可記錄所有合法字符的個數,然後按照 Num 和 Char這兩個數組中記錄每次循環輸出非0個數的字符並把個數減1,循環次數由記錄字符的最多個數決定,在記錄Num 和 Char 的時候可以設置一個變量 maxCount來記錄字符個數最多的個數。


當然,上述思想並不是最優的,因爲如果僅有一個字符串 aaaaaaaaaaaa,則需循環12次之多,很是浪費,你看完後是否有改進的想法呢?不妨留言評論和各位網友一起分享


編譯器: G++


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



源代碼如下

//source here
#include <iostream>
#include <cstring>
using std::cin;
using std::cout;
using std::endl;
#define N 500

int main(void)
{
    char str[N];

	char Num[10];
	char Char[26];

	while(cin.getline(str,N))
	{
		for(int i=0; i<10; i++)Num[i]=0;
		for(int i=0; i<26; i++)Char[i]=0;

		int flag=1,count,maxCount=0;
		count = strlen(str);

		for(int i=0;i<count;i++)
		{
			char c = str[i];
			if(c < '0' || c >'z' || (c > '9' && c < 'a'))
			{
				cout<<"<invalid input string>"<<endl;
				flag = 0;
				break;
			}
			else if(c >= '0' && c <= '9')
			{
				int p = c - '0';
				Num[p] = Num[p]+1;
				if(Num[p] > maxCount) maxCount = Num[p];
			}
			else
			{
				int p = c - 'a';
				Char[p] = Char[p]+1;
				if(Char[p] > maxCount) maxCount = Char[p];
			}
		}

		if(flag == 1)
		{
			int i=0;
			while(i<maxCount)
			{
				for(int j=0;j<10;j++)
				{
					if(Num[j]>0)cout<<char(j+'0');
					Num[j] = Num[j]-1;
				}
				for(int k=0;k<26;k++)
				{
					if(Char[k]>0)cout<<char(k+'a');
					Char[k] = Char[k]-1;
				}
				++i;
			}

			cout<<endl;

		}

	}
  
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章