zoukankan      html  css  js  c++  java
  • 腾讯2017暑期实习生编程题(3题)

      第一题:给定一个字符串s,你可以从中删除一些字符,使得剩下的串是一个回文串。如何删除才能使得回文串最长呢?输出需要删除的字符个数。

      思路:区间DP即可。我一开始想的是统计i~j内可以回文的字符个数,然后ans=len-dp[1][len]; 后来发现没法实现,即使是一个字符也是回文的这种情况也考虑到了也没办法做,因为我的思路是在dp[i][j]和dp[i+1][j-1]上进行,这样没有包括边界,这样做肯定是错的。比方说aab,在考虑外边的a和b时,如果这么转移应该是得到答案是2的,却得到了1(中间的a本身)。那么答案给出的方法是直接考虑dp[i][j]表示的是i~j内需要删除的个数,且考虑了边界。其代码如下:

     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <string.h>
     4 #include <string>
     5 #include <map>
     6 #include <vector>
     7 #include <iostream>
     8 using namespace std;
     9 typedef long long ll;
    10 const int N = 1000 + 5;
    11 
    12 int dp[N][N];
    13 char s[N];
    14 
    15 int main()
    16 {
    17     while(scanf("%s",s+1)==1)
    18     {
    19         int len = strlen(s+1);
    20         memset(dp,0,sizeof(dp));
    21         for(int i=2;i<=len;i++)
    22         {
    23             dp[i-1][i] = s[i-1]==s[i]?0:1;
    24             for(int j=i-2;j>=1;j--)
    25             {
    26                 if(s[j]==s[i]) dp[j][i] = dp[j+1][i-1];
    27                 else dp[j][i] = min(dp[j][i-1],dp[j+1][i]) + 1;
    28             }
    29         }
    30         printf("%d
    ",dp[1][len]);
    31     }
    32     return 0;
    33 }
    View Code

    那么,借鉴一下dp考虑边界的思路,再用我自己的方法,就也能通过了~我的代码如下:

     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <string.h>
     4 #include <string>
     5 #include <map>
     6 #include <vector>
     7 #include <iostream>
     8 using namespace std;
     9 typedef long long ll;
    10 const int N = 1000 + 5;
    11 
    12 int dp[N][N];
    13 char s[N];
    14 
    15 int main()
    16 {
    17     while(scanf("%s",s+1)==1)
    18     {
    19         int len = strlen(s+1);
    20         memset(dp,0,sizeof(dp));
    21         for(int i=1;i<=len;i++)
    22         {
    23             dp[i][i] = 1;
    24             dp[i][i+1] = s[i]==s[i+1]?2:1;
    25         }
    26         for(int i=3;i<=len;i++)
    27         {
    28             for(int j=1;j+i-1<=len;j++)
    29             {
    30                 if(s[j]==s[j+i-1]) dp[j][j+i-1] = dp[j+1][j+i-2] + 2;
    31                 else
    32                 {
    33                     dp[j][j+i-1] = max(dp[j][j+i-2],dp[j+1][j+i-1]);
    34                 }
    35             }
    36         }
    37         printf("%d
    ",len-dp[1][len]);
    38     }
    39     return 0;
    40 }
    View Code

    所以说这样就得到了一个区间dp需要注意的一点,有时候是要考虑边界的!

      第二题:小Q最近遇到了一个难题:把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,且不能申请额外的空间。你能帮帮小Q吗?

      思路:最初的想法就是直接写一个cmp:return (a>='a' && a<='z' && b>='A' && b<='Z'); 之后对字符串sort一下。思路看上去很棒,但是无法通过,有些长的会出现问题,原因的话我想是因为快排的实现原理吧:随机选一个位置的字符x,按照跟x的比较关系把序列分成小于x的和大于等于x的,然后把x放在这两段之间,然后分别递归做这两段。那么,就没办法保持小写字母和大写字母它们内部原先的顺序了,比方说我选出一个B,可以保证的是小写的全在它左边,但是大写字母和它没有比较关系,那么就乱了。

      但是,这个思路是值得借鉴的,只要手写个冒泡就能通过了。代码如下:

     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <string.h>
     4 #include <string>
     5 #include <map>
     6 #include <vector>
     7 #include <iostream>
     8 using namespace std;
     9 typedef long long ll;
    10 
    11 char s[1000+5];
    12 
    13 bool needChange(char a,char b)
    14 {
    15     return (a>='A' && a<='Z' && b>='a' && b<='z');
    16 }
    17 
    18 int main()
    19 {
    20     while(scanf("%s",s+1)==1)
    21     {
    22         int len = strlen(s+1);
    23         for(int i=1;i<=len-1;i++)
    24         {
    25             for(int j=1;j<=len-i;j++)
    26             {
    27                 if(needChange(s[j],s[j+1])) swap(s[j],s[j+1]);
    28             }
    29         }
    30         puts(s+1);
    31     }
    32 }
    View Code

      第三题:小Q今天在上厕所时想到了这个问题:有n个数,两两组成二元组,差的绝对值最小的有多少对呢?差的绝对值最大的呢?

      一开始想着先排序,然后各种lower_bound,upper_bound等等。。然后发现写起来非常烦,那么这里给出一种用map的方法,非常巧妙!代码如下:

     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <map>
     4 #include <string.h>
     5 #include <iostream>
     6 using namespace std;
     7 
     8 int main()
     9 {
    10     int n;
    11     while(cin>>n)
    12     {
    13         map<int,int> M;
    14         bool flag = false;  //flag表示有相同元素
    15         for(int i=1;i<=n;i++)
    16         {
    17             int x;
    18             cin>>x;
    19             if(M.find(x) == M.end()) M.insert(make_pair(x,1));
    20             else M[x]++,flag = true;
    21         }
    22 
    23         int ans1=0,ans2;
    24         int num = M.size();  //相差最大值的答案和元素的种类数有关
    25         if(num>1) ans2 = (*M.begin()).second * (*M.rbegin()).second;
    26             else ans2 = (*M.begin()).second * ((*M.begin()).second - 1)/2;
    27         if(flag)
    28         {
    29             for(map<int,int>::iterator it = M.begin();it!=M.end();it++)
    30             {
    31                 if((*it).second>1) ans1 += (*it).second * ((*it).second - 1)/2;
    32             }
    33         }
    34         else
    35         {
    36             int diff = (*M.rbegin()).second - (*M.begin()).second;
    37             for(map<int,int>::iterator it = M.begin(),it2 = ++M.begin();it2!=M.end();it++,it2++)
    38             {
    39                 int t = (*it2).first - (*it).first;
    40                 if(t < diff) ans1 = (*it2).second * (*it).second,diff = t;
    41                 else if(t == diff) ans1 += (*it2).second * (*it).second;
    42             }
    43         }
    44 
    45         cout<<ans1<<" "<<ans2<<endl;
    46     }
    47 }
    View Code

    现在,真心感受到了STL工具的强大!

     

  • 相关阅读:
    Hadoop系列:在Linux下部署hadoop 0.20.1
    EasyNet.Solr开发历程
    使用ios开发者证书
    多台电脑共用一个开发证书的方法
    ios学习第二天,类和属性
    ios开发第一天mvc介绍
    IOS的Bundle资源束制作
    ios证书申请,使用,调试,发布app
    paste命令
    shell的使用技巧
  • 原文地址:https://www.cnblogs.com/zzyDS/p/5651178.html
Copyright © 2011-2022 走看看