zoukankan      html  css  js  c++  java
  • 亚马逊在线笔试第二题的解2012.10.10

    原题(引用需注明为亚马逊笔试题):

    Question 2 / 2
    Question:

    As you know, two operations of Stack are push and pop. Now give you two integer arrays, one is the original array before push and pop operations, the other one is the result array after a series of push and pop operations to the first array. Please give the push and pop operation sequence.

    For example:

    If the original array is a[] = {1,2,3}, and the result array is b[] = {1,3,2}.

    Then, the operation sequence is “push1|pop1|push2|push3|pop3|pop2”(operations are split by ‘|’ and no space).

    Rules:

    The push and pop operations deal with the original int array from left to right.
    The input is two integer array. They are the original array and the result array. These interger array is split by space.
    The output is the operation sequence.
    If the original array cannot make to the result array with stack push and pop, The output should be 'None'.
    The operation "push1" means push the first element of the original array to the stack.
    The operation "pop1" means pop the first element of the original array from the stack, and add this element to the tail of the result array.
    Please don't include any space in the output string.
    Sample1:
    Input:
    1 2 3 4

    1 2 3 4
    Output:
    push1|pop1|push2|pop2|push3|pop3|push4|pop4

    Sample2:

    Input:

    1 2 3 4

    4 3 2 1
    Output:
    push1|push2|push3|push4|pop4|pop3|pop2|pop1

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    char* calculateOperationSequence(int *originalArray, int *resultArray, int length);
    
    inline bool isSpace(char x){
    return x == ' ' || x == '\r' || x == '\n' || x == '\r' || x == '\b' || x == '\t';
    }
    
    char * rightTrim(char *str){
    int len = strlen(str);
    while(--len>=0){
    if(isSpace(str[len])){
    str[len] = '\0';
    }else{
    break;
    }
    }
    return str;
    }
    
    char * getInputLine(char *buffer, int length){
    if(fgets(buffer,length, stdin)==NULL){
    return NULL;
    }
    rightTrim(buffer);
    if(strlen(buffer)<=0){
    return NULL;
    }
    return buffer;
    }
    
    int splitAndConvert(char* strings,int *array){
    char*tokenPtr = strtok(strings,",");
    int i=0;
    while(tokenPtr!=NULL){
    array[i] = atoi(tokenPtr);
    i++;
    tokenPtr=strtok(NULL,",");
    }
    return i;
    }
    
    int main(){
    char line[1000] = {0} ;
    while(getInputLine(line,1000)){
    int originalArray[30] = {0};
    int originalArrayLength = splitAndConvert(line,originalArray);
    if(originalArrayLength==0){
    break;
    }
    
    getInputLine(line, 1000);
    int resultArray[30] = {0};
    int resultArrayLength = splitAndConvert(line,resultArray);
    if(resultArrayLength==0){
    break;
    }
    
    char* operationSequence = calculateOperationSequence(originalArray, resultArray, resultArrayLength);
    cout<<operationSequence<<endl;
    }
    return 0; 
    }
    
    //your code is here
    char* calculateOperationSequence(int * originalArray, int * resultArray, int length)
    {
    
    }

    好吧,大致意思就是输入两个数组,第一个数组是栈的某个进入顺序,第二个数组是栈的某个输出顺序,然后让输出栈的进入和退出过程,需要检测错误输入的情况。今天花了一整天在真正搞定,实在是弱爆了。。strcat卡了半个小时,才发现new出来的char*需要str[0]=0才能使用strcat,真是坑爹坑死了!!数组下标又调试了俩小时,实在是有点崩溃,感觉有更好的递归算法可以用,但是却想不出来,就用了循环来做,如果有更好的解法,真心求指导啊~~~~~~~

    //your code is here
    char* calculateOperationSequence(int * originalArray, int * resultArray, int length)
    {
        char* str=new char[1000];
        str[0]=0;
        int i=0;
    
        while (i<length)
        {
            int rpos=-1;
            for (int j=length-1;j>=0;j--)
            {
                if (originalArray[i] == resultArray[j])//找到输出数组中originalArray[i]的位置
                {
                    rpos=j;
                    break;
                }
            }
    
            if (rpos == -1)//如果没找到返回错误
            {
                return "None";
            }
            else
            {
                for (int err=rpos-2;err>=0;err--)//错误输入顺序检测:如果原数组元素中的下一个数出现在了结果数组中该数位置的隔一个位置到0位置的某个位置,那么说明输入有错!比如原数组 1 2 3 结果数组却是 2 3 1 ,那么2出现在了结果数组中1隔一个位置,那么就说明有错
                {
                    if (originalArray[i+1]==resultArray[err])
                    {
                        return "None";
                    }
                }
            }
    
            int apos=i;//存储当前位置i,后面在apos和i之间一个来回输出push和pop
    
            while (i<length && rpos>=0)//如果原数组的下一个数是输出数组的上一个数,那么继续往下走,直到不相等
            {
                if (originalArray[++i]!=resultArray[--rpos])
                {
                    break;
                }
            }
    
                    //在apos和i之间输出push
            for (int out=apos;out<i;out++)
            {
                char push[20];
                sprintf(push,"push%d|",out+1);
                strcat(str,push);
            }
                    //在i到apos之间输出pop
            for (int out=i-1;out>=apos;out--)
            {
                char pop[20];
                if (i==length && out==apos)
                {
                    sprintf(pop,"pop%d",out+1);//最后一个pop后面不用加“|”
                }
                else
                {
                    sprintf(pop,"pop%d|",out+1);
                }
                strcat(str,pop);
            }
            
        }
    
        return str;
    }                        

     果然在计算机界一旦想要直捣黄龙就会出错的,犯了年轻人冲动自负的臭毛病,上面被划掉的解法除了让我学到一些字符串操作方法以外一文不值。待会儿给出真正的解代码,如果误人子弟了还请多多原谅。。。

    //your code is here
    char* calculateOperationSequence(int * originalArray, int * resultArray, int length)
    {
        char* result=new char[255];
        char pp[20];
        result[0]=0;
        int originalIndex=length-1,resultArrayIndex=0;
        std::stack<int> stack1,stack2;
    
        while(originalIndex>=0)
        {
            stack2.push(originalArray[originalIndex--]);
        }
    
        while (resultArrayIndex<length)
        {
            if (!stack1.empty() && stack1.top()==resultArray[resultArrayIndex])
            {
                sprintf(pp,"pop%d|",stack1.top());
                strcat(result,pp);
                stack1.pop();
            }
            else
            {
                while (!stack2.empty() && stack2.top()!=resultArray[resultArrayIndex])
                {
                    stack1.push(stack2.top());
                    sprintf(pp,"push%d|",stack2.top());
                    strcat(result,pp);
                    stack2.pop();
                }
                if (!stack2.empty())
                {
                    sprintf(pp,"push%d|",stack2.top());
                    strcat(result,pp);
                    sprintf(pp,"pop%d|",stack2.top());
                    strcat(result,pp);
                    stack2.pop();
                }
                else
                {
                    return "None";
                }
            }
            resultArrayIndex++;
        }
    
        return result;
    }

         这个应该没有问题了,去了实验室再调试一下,然后写点儿解释

  • 相关阅读:
    我的2017年总结
    iOS App图标和启动画面尺寸
    苹果手机屏幕一览
    纯 HTML5 APP与原生APP的差距在哪?
    设置全局导航栏颜色,标题大小和UIBarButtonItem字体大小
    OCiOS开发:CAGradientLayer 渐变色
    UICollectionViewCell的设置间距
    NSUserDefault的使用
    WKWebView进度及title
    TextView中的部分文字响应点击事件
  • 原文地址:https://www.cnblogs.com/darknightsnow/p/2718513.html
Copyright © 2011-2022 走看看