考研论坛

 
查看: 1117|回复: 0
打印 上一主题 下一主题

[信息院] 2019兰州大学计算机科学与技术复试机试试题

[复制链接]

1

主题

1

帖子

14

积分

新手上路

Rank: 1

精华
0
威望
2
K币
12 元
注册时间
2019-2-24
跳转到指定楼层
楼主
发表于 2019-3-17 10:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
A. Given a sequence of  numbers, find a sub-sequence of the numbers, whose summation is the largest.  
    For example, -2 11 -4 13 -2, the largest sub-sequence in summantion is 11 -4 13. We are required to output the value of the summation and the positions the sub-sequence begins and ends. So the programe's output comes as 20 2 4    Input format:    5

    -2 11 -4 13 -2
    output format:
    20 2 4
Here is my code
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<vector>
  5. #include<map>
  6. #include<set>
  7. #include<queue>
  8. using namespace std ;

  9. const int M = 2 * 10000 + 10 ;

  10. int p[M], q[M], A[M] ;

  11. int main(){
  12.     int N ;
  13.     cin>>N ;
  14.     for( int i = 0 ; i<N; i++ ){
  15.         cin>>A[i] ;
  16.     }
  17.     p[0] = A[0] ;
  18.     q[0] = 0 ;
  19.     for( int i = 1 ; i<N ; i ++ ){
  20.         if( p[i-1] <= 0 ){
  21.             p[i] = A[i] ;
  22.             q[i] = i ;
  23.         }
  24.         else {
  25.             p[i] = p[i-1] + A[i] ;
  26.             q[i] = q[i-1] ;
  27.         }
  28.     }
  29.     int val = p[0], bg = q[0], ed = q[0] ;
  30.     for( int i = 0 ; i<N ; i++ ){
  31.         if( p[i] > val ){
  32.             val = p[i] ;
  33.             bg = q[i] ;
  34.             ed = i ;
  35.         }
  36.     }
  37.     cout<<val<<" "<< bg+ 1<<" "<<ed + 1<<endl;
  38.     return 0 ;
  39. }
  40. /* Test example
  41. 4
  42. -2 11 -4 13
  43. */
复制代码




B: Given the frequency of every letter, use them to create a Haffman Tree and calculate the weighted path length. Output the weighted path length.
    Input format:
    8
    19 21 2 3 6 7 10 32
    output format:
    261
Here is my code
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<string>
  5. #include<vector>
  6. #include<stack>
  7. #include<map>
  8. #include<set>
  9. #include<queue>
  10. #include<cstdlib>
  11. #include<algorithm>
  12. using namespace std ;

  13. // weighted routing length of HAFFMAN Tree
  14. // 1. priority_queue to process constructing the tree
  15. //      1. each time get the top 2 node to and merge them to a new one and push it into the heap
  16. //      2. untill there is only one node left, for each new node, give a new tag and create a tree
  17. // 2. traverse the tree and tag the leaf node by the level of its position

  18. struct Node{
  19.     int val, pos ;
  20.     Node( int a , int b ): val( a ), pos( b){}
  21.     friend bool operator <( Node left, Node right){
  22.         if( left.val == right.val ) return left.pos > right.pos ;
  23.         return left.val > right.val ;
  24.     }
  25. } ;
  26. priority_queue< Node > pq ;

  27. const int M = 2 * 10000 + 10 ;
  28. vector<int> p[M] ;

  29. int q[M] ;
  30. int N , val , N1 ;

  31. int target( int father, int level ){
  32.     if( father < N )
  33.         return q[father] * level ;
  34.     return target( p[father][0], level + 1 ) + target( p[father][1] , level + 1 ) ;
  35. }

  36. int main(){
  37.     cin>> N ;
  38.     N1 = N ;
  39.     for( int i = 0 ; i < N ; i++ ){
  40.         cin>>q[i] ;
  41.         pq.push( Node(q[i], i ) ) ;
  42.     }
  43.     while( pq.size() > 1 ){
  44.         Node tmp1 = pq.top() ; pq.pop() ;
  45.         Node tmp2 = pq.top() ; pq.pop() ;
  46.         pq.push( Node( tmp1.val + tmp2.val , N1 ++ ) ) ;
  47.         p[ N1-1 ].push_back( tmp1.pos ) ;
  48.         p[ N1-1 ].push_back( tmp2.pos ) ;
  49.     }
  50.     // N1-1 is the father node
  51.     cout<< target( N1-1, 0 ) <<endl;
  52.     return 0 ;
  53. }
  54. /*
  55. Test example
  56. 8
  57. 19 21 2 3 6 7 10 32
  58. */
复制代码

C: Given a string, find the brackets that don't match, for example, )()(, the two brackets in the middle match but the two side, which are red as ) and ( don't have a half bracket to match. We replace ')' with letter '?' and '(' with letter '$'. For the other letters in the string, we replace them by blank space.
    Input format:
    )(ab)ab)ab(
    Output format:
    )(ab)ab)ab(
    ?          ?   $Here is my
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<vector>
  4. #include<map>
  5. #include<set>
  6. #include<stack>
  7. #include<algorithm>
  8. #include<queue>
  9. using namespace std ;

  10. /*Bracket Matching
  11. traverse the string,
  12.     for letter '(' , push it directly into the stack,
  13.     for letter ')' firstly, check whether the stack is empty,
  14.         otherwise, get the top letter, if the top letter is letter '(',  pop it out,
  15.         if not, push ')' into the stack
  16. */

  17. stack<int> stck ;

  18. int main(){
  19.     string str, cpy ;
  20.     while( getline(cin, str) ){
  21.         cpy = str ;
  22.         int len = str.length() ;
  23.         for( int i = 0; i < len ; i++ ){
  24.             if( str[i] == '(')
  25.                 stck.push( i ) ;
  26.             else if( str[i] == ')'){
  27.                 if( stck.empty() )
  28.                     stck.push( i ) ;
  29.                 else{
  30.                     if( str[ stck.top() ] == '(' )
  31.                         stck.pop() ;
  32.                     else
  33.                         stck.push( i ) ;
  34.                 }
  35.             }
  36.         }
  37.         while( !stck.empty()){
  38.             if( str[stck.top() ] == ')'){
  39.                 cpy[stck.top()] = '?' ;
  40.                 stck.pop() ;
  41.             }
  42.             else{
  43.                 cpy[stck.top()] = '


  44. ;
  45.                 stck.pop() ;
  46.             }
  47.         }
  48.         for( int i = 0 ; i<len ; i++ ){
  49.             if( ( cpy[i] == '?') ||( cpy[i] == '


  50. ) ){

  51.             }
  52.             else{
  53.                 cpy[i] = ' ' ;
  54.             }
  55.         }
  56.         cout<<str<<endl;
  57.         cout<<cpy<<endl;
  58.     }
  59.     return 0 ;
  60. }
复制代码




    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册 人人连接登陆

    本版积分规则   

    关闭

    您还剩5次免费下载资料的机会哦~

    扫描二维码下载资料

    使用手机端考研帮,进入扫一扫
    在“我”中打开扫一扫,
    扫描二维码下载资料

    关于我们|商务合作|小黑屋|手机版|联系我们|服务条款|隐私保护|帮学堂| 网站地图|院校地图|漏洞提交|考研帮

    GMT+8, 2026-1-13 14:38 , Processed in 0.067368 second(s), Total 9, Slave 8(Usage:6.5M, Links:[2]1,1_1) queries , Redis On.

    Powered by Discuz!

    © 2001-2017 考研 Inc.

    快速回复 返回顶部 返回列表
    × 关闭