小米服务端编程题
第一题:
不想说话,直接模拟就行了,乱写的:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
string className;
bool isDX(char a){
return a >= 'A' && a <= 'Z';
}
bool isXX(char a){
return a >= 'a' && a <= 'z';
}
bool isSZ(char a){
return a >= '0' && a <= '9';
}
char x2d(char a){
if(isXX(a)) return a -= 32;
return a;
}
int main(){
while(cin >> className){
vector<char> vec;
vec.push_back('_');
int len = className.length();
int flag = -1;
for(int i = 0;i < len;++ i){
if(className[i] == '.'){
vec.push_back('_');
flag = -1;
continue;
}
if(flag == 1 && !isXX(className[i])){
flag = -1;
vec.push_back('_');
}
else if(flag == 2 && !isSZ(className[i])){
flag = -1;
vec.push_back('_');
}
else if(flag == 3 && !isDX(className[i]) || flag == 3 && isDX(className[i]) && i + 1 < len && isXX(className[i + 1])){
flag = -1;
vec.push_back('_');
}
else if(flag == 4 && !isXX(className[i])){
flag = -1;
vec.push_back('_');
}
if(flag != -1){
vec.push_back(x2d(className[i]));
continue;
}
if(isDX(className[i]) && i + 1 < len && isXX(className[i + 1])){
flag = 1;
vec.push_back(className[i]);
}
else if(isXX(className[i])){
flag = 4;
vec.push_back(x2d(className[i]));
}
else if(isSZ(className[i])){
flag = 2;
vec.push_back(className[i]);
}
else {
flag = 3;
vec.push_back(x2d(className[i]));
}
}
vec.push_back('_');
for(int i = 0;i < vec.size();++ i){
cout << vec[i];
}
cout << endl;
}
return 0;
}
第二题:就是个求最长前缀,字典树水过的
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <map>
using namespace std;
const int SIZE = 120;
const int MAXN = 1e6 + 5;
const int INF = 0x3f3f3f3f;
struct TreeNode{
int id;
map<string, int> next;
void init(){
next.clear();
id = -INF;
}
} L[MAXN];
int tot;
string path;
int id;
void split(std::string& s,const std::string& delim,std::vector< std::string >& ret)
{
size_t last = 0;
size_t index=s.find_first_of(delim,last);
while (index!=std::string::npos)
{
ret.push_back(s.substr(last,index-last));
last=index+1;
index=s.find_first_of(delim,last);
}
if (index-last>0)
{
ret.push_back(s.substr(last,index-last));
}
}
void add_tree(string npath, int k){
vector<string> vec;
split(npath, "/", vec);
vec.erase(vec.begin());
int now = 0;
for(int i = 0;i < vec.size();++ i){
string tmp = vec[i];
//cout << vec[i] << endl;
int next = -1;
if(L[now].next.find(tmp) == L[now].next.end()){
next = ++ tot;
L[next].init();
L[now].next[tmp] = next;
}
else{
next = L[now].next[tmp];
}
now = next;
}
L[now].id = k;
}
int query(string npath){
vector<string> vec;
split(npath, "/", vec);
vec.erase(vec.begin());
int now = 0;
int v = -INF;
for(int i = 0;i < vec.size(); ++ i){
string tmp = vec[i];
int next = -1;
if(L[now].next.find(tmp) == L[now].next.end()){
if(L[now].id != -INF) v = L[now].id;
break;
}
else{
next = L[now].next[tmp];
}
now = next;
if(L[now].id != -INF) v = L[now].id;
}
return v;
}
int main(){
bool flag = false;
tot = 0;
L[tot].init();
while(cin >> path){
if(path[0] == '-'){
flag = true;
continue;
}
if(!flag){
cin >> id;
add_tree(path, id);
}
else{
int v = query(path);
cout << (v == -INF ? 0 : v) << endl;
}
}
return 0;
}
第三题:这个题很尴尬就过了33%,也懒得去优化了,让他懵逼去吧
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
const int SIZE = 120;
const int MAXN = 1e4 + 5;
const int INF = 0x3f3f3f3f;
string str;
char A[MAXN];
set<string> res;
char s2z(string c){
int a = atoi(c.c_str());
return a + 'a' - 1;
}
void DFS(char*nstr,int lk, int p, int& len){
if(p >= len){
nstr[lk] = '\0';
res.insert(string(nstr));
return;
}
nstr[lk] = s2z(str.substr(p, 1));
DFS(nstr,lk + 1,p + 1, len);
if(p + 1 < len){
nstr[lk] = s2z(str.substr(p, 2));
DFS(nstr,lk + 1, p + 2, len);
}
}
int main(){
while(cin >> str){
int len = str.length();
res.clear();
DFS(A,0, 0, len);
for(set<string>::iterator it = res.begin(); it != res.end();++ it){
set<string>::iterator iter = it;
iter ++;
cout << *it<< (iter == res.end() ? "\n" :" ");
}
}
return 0;
}
#小米#
