猿辅导后台开发笔试-第一题

楼主是个菜鸡,三道笔试题目一个都没通过。
交卷后仔细调试了第一题,字符压缩的问题。代码如下,

// 2019.8.4
#include<bits/stdc++.h>
using namespace std;
int main(){
    int c;
    cin >> c;
    string s;
    while(c--){
        cin >> s;
        string ans;
        string v;
        int i = 0;
        for(;i < s.size();){
            if(s[i] == '(' || (s[i] >= 'A' && s[i] <= 'Z')) {
                v += s[i++];
            } else if(s[i] == ')') {
                string t;
                while(v.back() != '(') {
                    t += v.back();
                    v.pop_back();
                }
                v.pop_back();
                reverse(t.begin(), t.end());
                // cout << t<< endl;
                string num;
                i++;
                while(s[i] == '0') i++;
                while(s[i] >= '0' && s[i] <= '9') {
                    num += s[i++];
                }
                int _num = stoi(num);
                string tt;
                while(_num) {
                    tt += t;
                    _num--;
                }
                v += tt;
            } else {
                string t;
                t = v.back();
                v.pop_back();
                string num;
                while(s[i] == '0') i++;
                while(s[i] >= '0' && s[i] <= '9') {
                    num += s[i++];
                }
                int _num = stoi(num);
                string tt;
                while(_num) {
                    tt += t;
                    _num--;
                }
                v += tt;
            }
//            cout << v << endl;
        }

        cout << v << endl;
    }
    return 0;
}
#猿辅导##笔试题目##春招##题解##秋招#
全部评论
后面两道题都没来得及看题目,没戏了
点赞 回复
分享
发布于 2019-08-04 11:07
第一题给的测试样例里面有一个A11B
点赞 回复
分享
发布于 2019-08-04 11:18
联易融
校招火热招聘中
官网直投
有java版吗,一顿操作猛如虎,一看测试通过 0%
点赞 回复
分享
发布于 2019-08-04 11:43
同昨天猿辅导的笔试,看了楼主的思路豁然开朗。答题的时候考虑用栈来做,感谢楼主提供的新思路。校招加油
点赞 回复
分享
发布于 2019-08-04 12:01
你这个 能过这种情况吗 ((A3V4)2)1
点赞 回复
分享
发布于 2019-08-04 15:41
贴一下我的code把233 #include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); for (int x=0; x<n; x++) { string s; cin >> s; int i = 0, l = s.size(); stack<string> st; while(i < l) { if(s[i] == '(') { st.push("("); i++; } else if(s[i] == ')') { string ans = ""; while(!st.empty() && st.top() != "(") { ans.insert(0,st.top()); st.pop(); } st.pop(); st.push(ans); i++; } else if(s[i] >= '0' && s[i] <= '9') { int ans = 0; while(i < l && s[i] >= '0' && s[i] <= '9') { ans = ans * 10 + (s[i] - '0'); i++; } string ss = st.top(); st.pop(); string tmp; for(int i=0; i<ans; i++) tmp += ss; st.push(tmp); } else { string ss; ss.push_back(s[i]); st.push(ss); i++; } } string res; while(!st.empty()) { res.insert(0, st.top()); st.pop(); } cout << res <<endl; } return 0; }
点赞 回复
分享
发布于 2019-08-04 15:46
public static void main(String[] args) { Scanner sc = new Scanner(System.in); int c = Integer.parseInt(sc.nextLine()); while(c>0) { c--; String inStr = sc.nextLine(); String res=""; int i = 0; for (; i < inStr.length();) { //1.如果遍历到的是 ( 或者字母 if(inStr.charAt(i)=='(' || (inStr.charAt(i)>='A' && inStr.charAt(i)<='Z')) { res = res + inStr.charAt(i++); } else if(inStr.charAt(i)==')'){ //2.如果遇到 ) String tmpStr=""; while (res.charAt(res.length()-1)!='(') { tmpStr = res.charAt(res.length()-1) + tmpStr; res = res.substring(0, res.length()-1);//去掉最后一个字符 } //直到遇到 ( res = res.substring(0, res.length()-1);//去掉最后一个字符 i++; String numStr = ""; //获取到括号内容后 处理括号外的数字 while(inStr.charAt(i)=='0')i++; while(i<inStr.length() && inStr.charAt(i)>='0'&& inStr.charAt(i)<='9') { numStr = numStr + inStr.charAt(i++); } int num = Integer.parseInt(numStr); String NTmpStr = ""; while (num>0) { //根据数字翻倍 NTmpStr = NTmpStr + tmpStr; num--; } res = res + NTmpStr; }else { //3.遇到字母后面的数字 String tmpStr = res.charAt(res.length()-1) + ""; res = res.substring(0, res.length()-1); String numStr = ""; //获取到括号内容后 处理括号外的数字 while(inStr.charAt(i)=='0')i++; while(i<inStr.length() && inStr.charAt(i)>='0'&& inStr.charAt(i)<='9') { numStr = numStr + inStr.charAt(i++); } int num = Integer.parseInt(numStr); String NTmpStr = ""; while (num>0) { //根据数字翻倍 NTmpStr = NTmpStr + tmpStr; num--; } res = res + NTmpStr; } } System.out.println(res); } }
点赞 回复
分享
发布于 2019-08-04 16:31

相关推荐

点赞 13 评论
分享
牛客网
牛客企业服务