2015年软件水平考试精选题(3)

时间:2015-04-02 15:55:00   来源:开云网页版     [字体: ]
-翻转句子中单词的顺序 [折叠]

  题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。句子中单词以空格符隔开。为简单起见,标点符号和普通字母一样处理。

  例如输入“I am a student.”,则输出“student. a am I”。

  分析:由于编写字符串相关代码能够反映程序员的编程能力和编程习惯,与字符串相关的问题一直是程序员笔试、面试题的热门题目。本题也曾多次受到包括微软在内的大量公司的青睐。

  由于本题需要翻转句子,我们先颠倒句子中的所有字符。这时,不但翻转了句子中单词的顺序,而且单词内字符也被翻转了。我们再颠倒每个单词内的字符。由于单词内的字符被翻转两次,因此顺序仍然和输入时的顺序保持一致。

  还是以上面的输入为例子。翻转“I am a student.”中所有字符得到“.tneduts a ma I”,再翻转每个单词中字符的顺序得到“students. a am I”,正是符合要求的输出。

  参考代码:

  ///////////////////////////////////////////////////////////////////////

  // Reverse a string between two pointers

  // Input: pBegin - the begin pointer in a string

  // pEnd - the end pointer in a string

  ///////////////////////////////////////////////////////////////////////

  void Reverse(char *pBegin, char *pEnd)

  {

  if(pBegin == NULL || pEnd == NULL)

  return;

  while(pBegin < pEnd)

  {

  char temp = *pBegin;

  *pBegin = *pEnd;

  *pEnd = temp;

  pBegin ++, pEnd --;

  }

  }

  ///////////////////////////////////////////////////////////////////////

  // Reverse the word order in a sentence, but maintain the character

  // order inside a word

  // Input: pData - the sentence to be reversed

  ///////////////////////////////////////////////////////////////////////

  char* ReverseSentence(char *pData)

  {

  if(pData == NULL)

  return NULL;

  char *pBegin = pData;

  char *pEnd = pData;

  while(*pEnd != '\0')

  pEnd ++;

  pEnd--;

  // Reverse the whole sentence

  Reverse(pBegin, pEnd);

  // Reverse every word in the sentence

  pBegin = pEnd = pData;

  while(*pBegin != '\0')

  {

  if(*pBegin == ' ')

  {

  pBegin ++;

  pEnd ++;

  continue;

  }

  // A word is between with pBegin and pEnd, reverse it

  else if(*pEnd == ' ' || *pEnd == '\0')

  {

  Reverse(pBegin, --pEnd);

  pBegin = ++pEnd;

  }

  else

  {

  pEnd ++;

  }

  }

  return pData;

  }
 判断整数序列是不是二元查找树的后序遍历结果[折叠]

  题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果。如果是返回true,否则返回false。

  例如输入5、7、6、9、11、10、8,由于这一整数序列是如下树的后序遍历结果:

  8

  / \

  6 10

  / \ / \

  5 7 9 11

  因此返回true。

  如果输入7、4、6、5,没有哪棵树的后序遍历的结果是这个序列,因此返回false。

  分析:这是一道trilogy的笔试题,主要考查对二元查找树的理解。

  在后续遍历得到的序列中,最后一个元素为树的根结点。从头开始扫描这个序列,比根结点小的元素都应该位于序列的左半部分;从第一个大于跟结点开始到跟结点前面的一个元素为止,所有元素都应该大于跟结点,因为这部分元素对应的是树的右子树。根据这样的划分,把序列划分为左右两部分,我们递归地确认序列的左、右两部分是不是都是二元查找树。

  参考代码:

  using namespace std;

  ///////////////////////////////////////////////////////////////////////

  // Verify whether a squence of integers are the post order traversal

  // of a binary search tree (BST)

  // Input: squence - the squence of integers

  // length - the length of squence

  // Return: return ture if the squence is traversal result of a BST,

  // otherwise, return false

  ///////////////////////////////////////////////////////////////////////

  bool verifySquenceOfBST(int squence[], int length)

  {

  if(squence == NULL || length <= 0)

  return false;

  // root of a BST is at the end of post order traversal squence

  int root = squence[length - 1];

  // the nodes in left sub-tree are less than the root

  int i = 0;

  for(; i < length - 1; ++ i)

  {

  if(squence[i] > root)

  break;

  }

  // the nodes in the right sub-tree are greater than the root

  int j = i;

  for(; j < length - 1; ++ j)

  {

  if(squence[j] < root)

  return false;

  }

  // verify whether the left sub-tree is a BST

  bool left = true;

  if(i > 0)

  left = verifySquenceOfBST(squence, i);

  // verify whether the right sub-tree is a BST

  bool right = true;

  if(i < length - 1)

  right = verifySquenceOfBST(squence + i, length - i - 1);

  return (left && right);

  }