哔哩哔哩第二次笔试 第三题key-value分割

很容易理解,就是一个表达式求值,使用两个栈记录操作数,两个操作符(:、#),Java版先写出来,完了只支持C++提交赶紧改了一下,没来得及提交,C++不熟悉
C++版
#include <iostream>
#include<string>
#include<vector>
#include<stack>
using namespace std;

class KeyValue{
public: 		
    string key;
    string value;
        
    KeyValue(string key,string value);
};

KeyValue::KeyValue(string key,string value){
	this->key=key;
	this->value=value;
}

void clearStack(stack<char>* stack){
	while(!stack->empty()){
		stack->pop();
	}
}

string getString(stack<char> stack){
    int keySize=stack.size();
    char keyChars[keySize];
    int index=keySize-1;
    while (!stack.empty()){
        keyChars[index]=stack.top();
        stack.pop();
        index--;
    }
    clearStack(&stack);
    string s(keyChars,keySize);
    return s;
}
    
    


vector<KeyValue> solution(string str){
    vector<KeyValue> result;
    stack<char> key;
    stack<char> value;
    bool flag=false;
    for(int i=0;i<str.size();i++){
        char current=str[i];
        if(current==' '){
            continue;
        }
        if(current==':'){
            if(key.empty()){
                flag=false;
                continue;
            }
            else{
                // 修改当前操作符
                flag=true;
            }
            continue;
        }
        else if(current=='#'){
            if(key.empty()&&value.empty()){
                continue;
            }
            if(!key.empty()&&!value.empty()){
                string keyString=getString(key);
                string valueString=getString(value);
                KeyValue keyValue(keyString,valueString);
                result.push_back(keyValue);
            }
            if(!key.empty()) {
                clearStack(&key);
            }
            if(!value.empty()) {
                clearStack(&value);
            }
            flag=false;
            continue;
        }

        if(flag){
            value.push(current);
        }
        else{
            key.push(current);
        }


    }

    if(!key.empty()&&!value.empty()){
        string keyString=getString(key);
        string valueString=getString(value);
        KeyValue keyValue(keyString,valueString);
        result.push_back(keyValue);
    }
    return result;
}
    
    
int main() {
    string a;
    while(cin >> a ){
    	vector<KeyValue> result=solution(a);
    	cout<<result.size()<<endl;
    	for(auto it=result.begin();it != result.end();++it){
			cout<<it->key<<" "<<it->value<<endl;
		}
	}        
}
Java版
package bilibili;

import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;

public class Third {
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        String str=scanner.nextLine();
        List<KeyValue> result=solution(str);
        System.out.println(result);
    }
    public static List<KeyValue> solution(String str){
        List<KeyValue> result=new ArrayList<>();
        Stack<Character> key=new Stack<>();
        Stack<Character> value=new Stack<>();
        boolean flag=false;
        for(int i=0;i<str.length();i++){
            char current=str.charAt(i);
            if(current==' '){
                continue;
            }
            if(current==':'){
                if(key.isEmpty()){
                    flag=false;
                    continue;
                }
                else{
                    // 修改当前操作符
                    flag=true;
                }
                continue;
            }
            else if(current=='#'){
                if(key.isEmpty()&&value.isEmpty()){
                    continue;
                }
                if(!key.isEmpty()&&!value.isEmpty()){
                    System.out.println(key);
                    System.out.println(value);
                    String keyString=getString(key);
                    String valueString=getString(value);
                    KeyValue keyValue=new KeyValue(keyString.trim(),valueString.trim());
                    result.add(keyValue);
                }
                if(!key.isEmpty()) {
                    key.clear();
                }
                if(!value.isEmpty()) {
                    value.clear();
                }
                flag=false;
                continue;
            }

            if(flag){
                value.push(current);
            }
            else{
                key.push(current);
            }


        }

        if(!key.isEmpty()&&!value.isEmpty()){
            String keyString=getString(key);
            String valueString=getString(value);
            KeyValue keyValue=new KeyValue(keyString.trim(),valueString.trim());
            result.add(keyValue);
        }
        return result;
    }

    public static String getString(Stack<Character> stack){
        int keySize=stack.size();
        char[] keyChars=new char[keySize];
        int index=keySize-1;
        while (!stack.isEmpty()){
            keyChars[index--]=stack.pop();
        }
        return String.valueOf(keyChars);
    }

    static class KeyValue{
        String key;
        String value;
        public KeyValue(String key,String value){
            this.key=key;
            this.value=value;
        }

        public String toString(){
            StringBuilder strb=new StringBuilder();
            strb.append(key);
            strb.append(" ");
            strb.append(value);
            return strb.toString();
        }
    }
}
哔哩哔哩去不了了,0 60 0提交代码,尽力了!


#笔试题目##哔哩哔哩##Java##C/C++#
全部评论
我记得题目中说的是分割符是输入的第一列和第二列,不能直接等于#和:
点赞 回复
分享
发布于 2019-09-10 22:47

相关推荐

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