[牛客练习] 深信服校园招聘c/c 软件开发H卷
Apare
2020.9.17
牛客链接
56种
dp或者dfs
#include <bits/stdc++.h>
using namespace std;
int r[10];
int cnt;
void dfs(int x) {
if(x==5) {
if(r[4]==6) {
++cnt;
}
return;
}
for(int i=r[x-1];i<=6;++i) {
r[x] = i;
dfs(x+1);
}
}
int main(void) {
r[0] = 1;
dfs(1);
//4,6
cout<<cnt<<endl;
return 0;
}
访问权限 <–
他的题意应该是这样的:同一个路径,如果之前指定过权限,就不再覆盖权限。如果没有指定过,就继承父节点的权限。默认根节点的权限是N
用Trie树维护即可。可以把路径处理成vector<string>
, Trie树的每个结点存储一个string 和该路径的权限。
#include <bits/stdc++.h>
using namespace std;
vector<string> Split(const string& s) {
vector<string> ans;
int len = s.length();
string tmp;
for(int i=1;i<len;++i) {
if(s[i]=='/') ans.push_back(tmp),tmp="";
else tmp += s[i];
}
if(tmp.size()) ans.push_back(tmp);
return ans;
}
struct Trie_Node{
string name; //该结点的文件名
char tag; //Y or N
unordered_map<string,Trie_Node*> child;
Trie_Node(string tmp,char ch) {
name = tmp; tag = ch;
}
};
//void dfs(Trie_Node * root,vector<Trie_Node*>& v) {
// cout<<"dfs("<<root->name<<")\n";
// if(!root)
// {
// for(auto x:v) {
// cout<<x->name<<"("<<x->tag<<") -> ";
// }
// cout<<endl;
// return;
// }
// v.push_back(root);
// if(root->child.size()==0) {
// cout<<"[";
// for(auto x:v) {
// cout<<"name:("<<x->name<<"),tag:"<<"("<<x->tag<<") ";
// }
// cout<<"]\n";
// v.pop_back();
// return;
// }
// for(auto x:root->child) {
if(x.second)
// dfs(x.second,v);
// }
// v.pop_back();
//
//}
struct Trie{
struct Trie_Node * root; //根结点
Trie() {
root = new Trie_Node(string("xzc"),'N');
}
void Destroy(Trie_Node * root) {
for(auto x:root->child) {
Destroy(x.second);
}
delete root;
root = nullptr;
}
~Trie() {
Destroy(root);
}
void Insert(const vector<string>& v,char tage) {
//插入字典树
Trie_Node * now = root;
if(!v.size()) {
if(tage!=' ') root->tag = tage;
return;
}
for(int i=0;i<(int)v.size();++i) {
auto it = now->child.find(v[i]);
if(it==now->child.end()) {
if(tage!=' ') return;
Trie_Node * p = new Trie_Node(v[i],' ');
now->child[v[i]] = p;
now = p;
}
else {
now = it->second;
}
}
if(tage!=' '&&now->tag==' ') now->tag = tage;
}
char Query(const vector<string>& v) {
Trie_Node * now = root;
for(int i=0;i<(int)v.size();++i) {
auto it = now->child.find(v[i]);
if(it->second->tag==' ') it->second->tag = now->tag;
now = it->second;
}
return now->tag;
}
}trie;
vector<vector<string> > vstr;
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
int n,m;
cin>>n;
string str;
for(int i=1;i<=n;++i) {
cin>>str;
vector<string> v = Split(str);
vstr.push_back(v);
trie.Insert(v,' ');
}
cin>>m;
char flag;
for(int i=1;i<=m;++i) {
cin>>flag;
cin>>str;
vector<string> v = Split(str);
trie.Insert(v,flag);
}
for(int i=0;i<n;++i)
cout<<trie.Query(vstr[i])<<"\n";
// vector<Trie_Node*> v;
// dfs(trie.root,v);
return 0;
}
/*
链接:https://www.nowcoder.com/questionTerminal/d87d5dacfd4e4d45b636132f1af85c2f
来源:牛客网
用例:
3
/mem/total
/s
/mem/totals
5
Y /mem/daemons/findme
N /mem/daemons
Y /mem/totals
Y /me
Y /
对应输出应该为:
Y
Y
Y
你的输出为:
N
N
Y
*/
/*
链接:https://www.nowcoder.com/questionTerminal/d87d5dacfd4e4d45b636132f1af85c2f
来源:牛客网
2
/mem/total/find
/mem/total/f
8
Y /mem/daemons/findme
N /mem/daemons
Y /mem/totals
Y /mem/total/find
Y /mem
Y /
Y /mem/total
N /mem/total/find
对应输出应该为:
Y
Y
*/
手机号查询
把手机号的所有子串截取出来,去重。用map或者unordered_map给子串计数。
注意,如果出现了重复的手机号,只算一个。
#include <bits/stdc++.h>
using namespace std;
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
string s;
int n,m;
cin>>n>>m;
unordered_map<string,int> exist;
unordered_map<string,int> mp;
for(int i=1;i<=n;++i) {
cin>>s;
if(exist.find(s)!=exist.end()) continue;
exist[s] = 1;
set<string> st;
for(int i=0;i<11;++i) {
string tmp;
for(int j=i;j<11;++j) {
tmp += s[j];
st.insert(tmp);
}
}
for(auto x:st) {
++mp[x];
}
}
while(m--) {
cin>>s;
auto it = mp.find(s);
if(it==mp.end()) cout<<"0\n";
else cout<<it->second<<"\n";
}
return 0;
}
数字序列
可以三个一组,看成一个三为整数,然后用哈希存储个位数的下表。然后找循环节。
1 1 1 3 5 9 17 17 15 13 9 13 13 7 ,
感觉样例有问题。
#include <bits/stdc++.h>
using namespace std;
int main(void) {
// freopen("in.txt","r",stdin);
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int T,n,x,y,z;
cin>>T;
while(T--) {
cin>>x>>y>>z>>n;
string s = to_string(x)+to_string(y)+to_string(z);
unordered_map<int,int> mp;
mp[x*100+y*10+z] = 2;
while(1) {
int sz = s.length();
x = (s[sz-1]-'0')+(s[sz-2]-'0')+(s[sz-3]-'0');
s += to_string(x);
if(x<10) {
y = (int)(s[sz-2]-'0')*100+(int)(s[sz-1]-'0')*10+x;
auto it = mp.find(y);
if(it!=mp.end()&&it->second+3<=sz) {
int pos = it->second;
int len = sz - pos;
n -= pos+1;
cout<<s[pos+n%len]<<endl;
goto Line;
}
mp[y] = sz;
}
++sz;
if(x>=10) {
y = (int)(s[sz-2]-'0')*100+(int)(s[sz-1]-'0')*10+x%10;
auto it = mp.find(y);
if(it!=mp.end()&&it->second+3<=sz) {
int pos = it->second;
int len = sz - pos;
n -= pos+1;
cout<<s[pos+n%len]<<endl;
goto Line;
}
mp[y] = sz;
}
if((int)s.size()>=n) {
cout<<s[n-1]<<endl;
goto Line;
}
}
Line:;
}
return 0;
}
/*
链接:https://www.nowcoder.com/questionTerminal/6f91f0b992a14333ba1aedff2fe73794?f=discussion
来源:牛客网
10
2 6 8 972307520
7 0 9 81366660
5 2 6 729532386
7 6 1 515853342
5 5 1 11691568
4 7 4 156507358
1 5 7 245505930
8 0 0 95224738
5 9 2 197774376
2 4 8 135229014
6
0
2
2
3
1
0
6
0
1
*/
2020.9.18 0:17
xzc