您的位置:首页 > 编程语言

2018美团点评笔试(机器学习,数据挖掘方向)编程题题解

2018-03-22 22:04 791 查看
题目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].如此即可算出答案。
代码如下:/*by kzl*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>

using namespace std;
const int maxx = 1e5+500;
const int INF = 0x3f3f3f3f;
typedef long long LL;

string str;
string str2;
int a[maxx],b[maxx];
int main(){
cin>>str>>str2;
int len = str.length();
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
int aa=0,bb=0;
for(int i=0;i<len;i++){
if(str[i]=='a')aa++;
else bb++;
a[i] = aa;b[i] = bb;
}
int len2 = str2.length();
int sum = 0;
for(int j=0;j<len2;j++){
if(str2[j]=='a'){
if(j==0)sum+=b[j+len-len2];
else sum+=b[j+len-len2]-b[j-1];
}
else {
if(j==0)sum += a[j+len-len2];
else sum += a[j+len-len2]-a[j-1];
}
}
cout<<sum<<endl;
return 0;
}

 题目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:
/*by kzl*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>

using namespace std;
const int maxx = 1e5+500;
const int INF = 0x3f3f3f3f;
typedef long long LL;

string str;
int ch[12];
int main(){
cin>>str;
int len = str.length();
memset(ch,0,sizeof(ch));
for(int i=0;i<len;i++){
int c = str[i]-'0';
ch[c]++;
}
int mi = INF;int index=0;
for(int i=1;i<10;i++){
if(ch[i]<mi){mi = ch[i];index = i;}
}
if(mi==0)cout<<index<<endl;
else if(mi==1){
if(ch[0]==0)cout<<index<<"0"<<endl;
else cout<<index<<index<<endl;
}
else {
if(ch[0]<mi){
cout<<"1";
for(int i=0;i<ch[0]+1;i++)cout<<"0";
cout<<endl;
}
else{
cout<<index;
if(ch[0]==0)cout<<"0";
else cout<<index;
for(int i=1;i<mi;i++)cout<<index;
cout<<endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  美团 面试题 春招