×

Loading...

Topic

This topic has been archived. It cannot be replied.
  • 工作学习 / IT技术讨论 / a c++ test for c++ friends
    Implement the following generic Standard Library function defined in <algo>:
    template <class Src1, class Src2>
    Src1 search (Scr1 i, Src1 j, Src2 p, Src2 q);
    // Searches the sequence i…(j-1) for a subsequence that equals
    // the sequence p…(q-1). If found, position k where it begins
    // is returned; otherwise, j is returned.
    // Invariant: no elements are changed.

    I cannot understand the meaning. How can I find a Src2 sequence in a Src1 sequence? How to access to next Src1 from (Src1 i) to (Src1 j).
    Who could give the Implement? Thanks in advance!
    • I can tell u the result, but could u tell me where the question is from first?
      • I got it from a job agency.
        • ok, I am going to explain it for u, hold on!
    • 它的意思是要查找sequence from i to j 中,(查找时实际上是i to j-1,)的subsequence p to q(查找时是p to q-1) , 如果找到了子序列数,那么返回找到的位置,反之返回j. 这是一个template class. 以下代码仅供参考.
      Src1 search (Scr1 i, Scr1 j, Scr2 p, Scr2q);
      {
      Src1 tSrc1, mSrc1=i;
      Src2 Src2=p;
      while(mSrc1!=j){
      tSrc1=mSrc1;
      tSrc2=p;
      while((*tSrc1==*tScr2)&&(tSrc2!=q)&&(tSrc1=q))
      {++tSrc1;++tSrc2;}
      if (tSrc==q)
      return mSrc1;
      mSrc1++;
      }
      return j;
      }
      }
      }
      • Sorry, 程序有些错误,更正如下.
        Src1 search (Src1 i, Src1 j, Src2 p, Src2q);
        {
        Src1 tSrc1, mSrc1=i;
        Src2 tSrc2=p;
        while(mSrc1!=j){
        tSrc1=mSrc1;
        tSrc2=p;
        while((*tSrc1==*tScr2)&&(tSrc2!=q)&&(tSrc1=q))
        {++tSrc1;++tSrc2;}
        if (tSrc==q)
        return mSrc1;
        mSrc1++;
        }
        return j;
        }
        }
        • 怪不得我这个门外汉昨天看了半天看不懂呢。
          • David, 那仅仅是一点笔误,无伤大碍哟!
            • 好精神!!!
              • 我来加之后时差就没有倒过来过,漫漫长夜,死活睡不着.
                • 那你索性想我这样,找个上晚班的工作,不用倒时差
          • This program is still incorrect
            Src1 search (Src1 i, Src1 j, Src2 p, Src2q);
            {
            Src1 tSrc1, mSrc1=i;
            Src2 tSrc2=p;
            while(mSrc1!=j){
            tSrc1=mSrc1;
            tSrc2=p;
            while((*tSrc1==*tScr2)&&(tSrc2!=q)&&(tSrc1!=j))
            {
            ++tSrc1;
            ++tSrc2;
            }
            if ((tSrc2==q)&&(*tScr1==*tScr2))
            return mSrc1;
            mSrc1++;
            }
            return j;
            }
            }
    • 哈哈,我也被考了这个题目,哈哈哈哈,我知道你是哪家公司。可惜没有人根俺合作啊,看来只有靠自己。:(
      • I sent u email, u can check the information.
        • 没收到,请发sinopatriot@yahoo.com
          • did u recieve my email?
        • 回答一下这个题目吧,跟那个差不多哩,让受苦受难的兄弟们共享。。。
          Implement the following generic Standard Library function defined in <algo>:
          template <class Src>
          bool equal (Src I, Src j, Src p);
          // Returns true iff the subsequence i…(j-1) is the same
          // as the subsquence p…(p+1)where n = i - j.
          // Invariant: no elements are changed.
          //---------------------------
          • no,这个比那个要复杂些,HOLD ON!
            • 代码如下也许有错误不过问题不会很大.
              template <class Src> bool equal (Src I, Src j, Src p){
              while ((i!=j) && ((*i)==(*p))) { ++i; ++p; }
              if (i==j) return true;
              else return false;
              }
    • 第二道题的subsquence p..(p+1), 应该是p...(p+n).
      • 原题如此
        • 他写错了而且条件应该是n=j-i, 实际上这题比第一要简单因为不管p+n大于i to j还是小于我们只需从p开始查找,直到找到符合p+j-i的position就行了,这是个人意见.
    • Thanks both of you. Could I contact with both of you,fang and 西西先生?My e-mail:stevenzhu2000@hotmai.com
      • 我还以为您老在国内呢,这么长时间没露脸.
      • 我的邮件,sinopatriot@yahoo.com
    • Steven, 在Chinasmile 的问题我在这里解答了. 关于STREAM我想你在相关书上能找到答案吧 What are the advantages and disadvantages of using a Binary Search on a tree? What is the difference between a vector and an array?
      The advantage of binary search tree is only take o(finding) time to insert and delete one element, but because it is not a balance binary search tree, so the height of tree can be o(n) , it will take o(n) time to finding an element.

      A vector looks like an expandable array , but vector provide functions to insert and delete element by postion. Array can only be passed by reference, but vector can be passed by reference and value. Array can not be return value of a function , but vetctor may be the return value of a function.