美团春招编程题

题目1:给出两个相同长度的由字符 a 和 b 构成的字符串,定义它们的距离为对应位置不同的字符的数量。如串”aab”与串”aba”的距离为 2;串”ba”与串”aa”的距离为 1;串”baa”和串”baa”的距离为 0。下面给出两个字符串 S 与 T,其中 S 的长度不小于 T 的长度。我们用|S|代表 S 的长度,|T|代表 T 的长度,那么在 S 中一共有|S|-|T|+1 个与 T 长度相同的子串,现在你需要计算 T 串与这些|S|-|T|+1 个子串的距离的和。
输入
第一行包含一个字符串 S。

第二行包含一个字符串 T。

S 和 T 均由字符 a 和 b 组成,1 ≤ |T| ≤ |S| ≤105 。

输出
输出对应的答案。

样例输入
aab
aba
样例输出
2

Hint
Input Sample 2
aaabb
bab
Output Sample 2
5
在样例 2 中,”aaa”与”bab”的距离为 2,”aab”与”bab”的距离为 1,”abb”与”bab”的距离为 2,
所以最后答案为 5。

解析:这一题听同学说暴力会超时。。。。其实思路也不难,如下:

设:第一个字符串长度为len,第二个字符串长度len2,那么这样思考,既然第二个字符串B(短的那个),会匹配长字符的【0,len-len2】的位置,那么对于短字符串的第一字符,它会与长字符的【0,len-len2】位置匹配,对于短字符的第二个字符,他会与长字符的【1,len-len2+1】位置匹配,对于短字符的第三个字符,他会与长字符的【2,len-len2+2】位置匹配。。。。。。那么我们只需用a[i]记录长字符[0,i]中a的个数,b[i]记录长字符[0,i]的个数,然后对于短字符的第x个字符B[x]而言,它匹配的是长字符的第【x,x+len-len2】位置,那么如果B[x]==a,用sum记录总的不匹配个数,sum+=b[x+len-len2]-b[x];如果是B[x]==b,sum+=a[x+len-len2]-a[x].如此即可算出答案。

代码如下:

 
  1. /*by kzl*/

  2. #include<iostream>

  3. #include<cstring>

  4. #include<cstdio>

  5. #include<algorithm>

  6. #include<cmath>

  7. #include<queue>

  8. #include<stack>

  9. #include<vector>

  10.  
  11. using namespace std;

  12. const int maxx = 1e5+500;

  13. const int INF = 0x3f3f3f3f;

  14. typedef long long LL;

  15.  
  16. string str;

  17. string str2;

  18. int a[maxx],b[maxx];

  19. int main(){

  20. cin>>str>>str2;

  21. int len = str.length();

  22. memset(a,0,sizeof(a));

  23. memset(b,0,sizeof(b));

  24. int aa=0,bb=0;

  25. for(int i=0;i<len;i++){

  26. if(str[i]=='a')aa++;

  27. else bb++;

  28. a[i] = aa;b[i] = bb;

  29. }

  30. int len2 = str2.length();

  31. int sum = 0;

  32. for(int j=0;j<len2;j++){

  33. if(str2[j]=='a'){

  34. if(j==0)sum+=b[j+len-len2];

  35. else sum+=b[j+len-len2]-b[j-1];

  36. }

  37. else {

  38. if(j==0)sum += a[j+len-len2];

  39. else sum += a[j+len-len2]-a[j-1];

  40. }

  41. }

  42. cout<<sum<<endl;

  43. return 0;

  44. }

 

 

题目2:

在十进制表示中,任意一个正整数都可以用字符‘0’-‘9’表示出来。但是当‘0’-‘9’这些字符每种字符的数量有限时,可能有些正整数就无法表示出来了。比如你有两个‘1’ ,一个‘2’ ,那么你能表示出 11,12,121 等等,但是无法表示出 10,122,200 等数。现在你手上拥有一些字符,它们都是‘0’-‘9’的字符。你可以选出其中一些字符然后将它们组合成一个数字,那么你所无法组成的最小的正整数是多少?输入第一行包含一个由字符’0’-‘9’组成的字符串,表示你可以使用的字符。· 1 ≤字符串长度≤ 1000输出输出你所无法组成的最小正整数。样例输入55样例输出1HintInput Sample 2123456789Output Sample 210

解析:

这题相对来说还是比较简单的,不过我写的太复杂了。。。

要考虑一下几种情况:

1.找到1-9中出现次数最少的index,个数为mi,如果mi为0的话,直接输出一个index

2如果0的次数比他们的少,那么直接就可以输出1+0的个数加一个“0”

3.如果0的个数不比他们少,那么就输出一个index,在输出一个0,然后输出mi-1个index就好了。

代码长的比人丑多了QAQ:

[cpp] view plain copy

  1. <code class="language-cpp">/*by kzl*/  
  2. #include<iostream>  
  3. #include<cstring>  
  4. #include<cstdio>  
  5. #include<algorithm>  
  6. #include<cmath>  
  7. #include<queue>  
  8. #include<stack>  
  9. #include<vector>  
  10.   
  11. using namespace std;  
  12. const int maxx = 1e5+500;  
  13. const int INF = 0x3f3f3f3f;  
  14. typedef long long LL;  
  15.   
  16. string str;  
  17. int ch[12];  
  18. int main(){  
  19.     cin>>str;  
  20.     int len = str.length();  
  21.     memset(ch,0,sizeof(ch));  
  22.     for(int i=0;i<len;i++){  
  23.         int c = str[i]-'0';  
  24.         ch[c]++;  
  25.     }  
  26.     int mi = INF;int index=0;  
  27.     for(int i=1;i<10;i++){  
  28.         if(ch[i]<mi){mi = ch[i];index = i;}  
  29.     }  
  30.     if(mi==0)cout<<index<<endl;  
  31.     else if(mi==1){  
  32.         if(ch[0]==0)cout<<index<<"0"<<endl;  
  33.         else cout<<index<<index<<endl;  
  34.     }  
  35.     else {  
  36.             if(ch[0]<mi){  
  37.                 cout<<"1";  
  38.                 for(int i=0;i<ch[0]+1;i++)cout<<"0";  
  39.                 cout<<endl;  
  40.             }  
  41.             else{  
  42.             cout<<index;  
  43.             if(ch[0]==0)cout<<"0";  
  44.             else cout<<index;  
  45.             for(int i=1;i<mi;i++)cout<<index;  
  46.             cout<<endl;  
  47.             }  
  48.     }  
  49. return 0;  
  50. }  
  51. </code> 

猜你喜欢

转载自blog.csdn.net/u012879957/article/details/82597356