小米服务端编程题

第一题:
不想说话,直接模拟就行了,乱写的:
#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;
}

#小米#
全部评论
#include <iostream> #include <set> using namespace std; string instr; void decrypt(int ith, string nowstr, set<string> &v) {     if(ith >= instr.size())     {         v.insert(nowstr);         return;     }     int num;     for(int i = 1; i <= 2; i++)     {         if(instr[ith] == '0')             break;         if(i == 1)             num = instr[ith] - '0';         else         {             if(ith >= (instr.size() - 1))                 break;             num = (instr[ith] - '0') * 10 + (instr[ith + 1] - '0');             if(num > 26)                 break;         }         char temp = 'a' + (num - 1);         decrypt(ith + i, nowstr + temp, v);     } } int main() {     while(cin >> instr)     {         set<string> result;         decrypt(0, "", result);         set<string>::iterator it;         for(it = result.begin(); it != result.end(); it++) {             if(it != result.begin())                 cout << ' ';             cout << *it;         }         cout << endl;     }     return 0; } 这是我ac了的代码,毕竟考试写的,可读性绝对是差,你可以看看错哪了……看不明白可以问我……
点赞 回复 分享
发布于 2017-09-19 09:39
2.3就挂了?非得全ac才能过吗?
点赞 回复 分享
发布于 2017-09-18 22:25
厉害了,,,不知道有isdigit这些函数吗。。
点赞 回复 分享
发布于 2017-09-18 21:42
哦,第三题估计是要直接输出字符串的,额,懒得输了,哎,挂了,回家种田
点赞 回复 分享
发布于 2017-09-18 21:27

相关推荐

02-17 20:43
西北大学 Java
在做测评的猫头鹰很紧张:他问你,你问deep seek
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务