这次虾皮二面和读研研究方向有点相关,最顺畅的一次面试,然鹅我把面试官鸽了......
自我介绍完,问了几个题目后,出了个编程题:
给定一个字符串 s 和一些 长度相同 的单词 words 。找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置。
输入:s = "hello,worldhellworldhellowordhiworld", words = ["hello","world"],输出:[16,21,26]。
注意子串要与 words 中的单词完全匹配,中间不能有其他字符 ,但不需要考虑 words 中单词串联的顺序。
我就一心写代码去了,断网了、电脑断电了没发现(房间没停电),
等我终于写完了字典树准备调试的时候才发现电脑断电了只有38%了,一直检查充电器,后来才发现刚开始写代码时就断网了,
面试官人脸退出去时我还觉得奇怪,后来足足等了我20分钟,又过了7分钟我才终于回到房间。
然后给hr发邮件说了一下情况:
然后我一个人在房间默默debug,10分钟后通过了三个测试用例。面试官大概会以为我做不出来就不辞而别了吧。
hhh
后来就去发朋友圈了,还没写完,面试官又进来了:
然后我们继续聊了近20分钟,以为我是有事打电话去了,说打电话也打不通。
最后贴一下代码:
#include<iostream>
#include<string>
#include<vector>
using namespace std;
typedef struct node{
int count=0;
node* children[26];
};
int wordNum=0;
int lenWord=0;
void insert(node* root, string s){
node* p=root;
for(const auto& c:s){
if(p->children[c-'a']==nullptr){
p->children[c-'a']=new node;
}
p=p->children[c-'a'];
}
p->count++;
}
void dfs_query(vector<int>& ans, node* root,const string s,int findCount,int fromI,int nowI){
if(findCount==wordNum){
ans.push_back(fromI);
return;
}
else if(nowI==s.length()){
return;
}
int si=nowI;
node* p=root;
bool goLenWord=true;
for(int i=0;i<lenWord;i++){
if(si<s.length() && p->children[s[si]-'a']!=nullptr){
p=p->children[s[si]-'a'];
si++;
}else{
goLenWord=false;
break;
}
}
if(goLenWord&&p->count){
p->count--;
//cout<<p->count<<"\n";
dfs_query(ans,root,s,findCount+1,fromI,si);
p->count++;
}
}
int main(){
node* root=new node;
vector<int> ans;
string s,t;
cin>>s;
vector<string> words;
while(cin>>t)words.emplace_back(t);
wordNum=words.size();
lenWord=words[0].size();
for(const auto& w:words)
insert(root,w);
for(int i=0;i<s.length()-words.size()*lenWord;i++)
dfs_query(ans,root,s,0,i,i);
cout<<ans.size()<<"\n";
for(const auto& si:ans)
cout<<si<<", ";
return 0;
}
没想到今晚两个伙伴来微信找我又帮我内推字节hhh,12月面了1次,3月面了两个二面...