2012 亚马逊在线笔试题目1
2014-12-12 11:59
211 查看
进栈出栈问题
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:
Time Remaining: 00:25:17
1. The push and pop operations deal with the original int array from left to right.
2. The input is two integer array. They are the original array and the result array. These interger array is split by space.
3. The output is the operation sequence.
4. If the original array cannot make to the result array with stack push and pop, The output should be 'None'.
5. The operation "push1" means push the first element of the original array to the stack.
6. 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.
7. 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
#include < iostream >
#include < cstring >
#include < cstdio >
#include < cstdlib >
#include < stack >
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);
if (NULL != operationSequence)
{ // 原来系统提供的代码。这里没有NULL判断
cout<< operationSequence <<endl;
free(operationSequence); // 自己加的
}
else
cout<< "None" <<endl; // 自己加的
}
return 0;
}
//your code is here
//下面才是让写代码的地方,其他的系统已经自动给出。
char* calculateOperationSequence(int* originalArray, int* resultArray, int length)
{
if(NULL==originalArray || NULL==resultArray || length <= 0)
return NULL;
stack mystack;
int resultindex=0,origindex=0;
int index=0;
string operations("");
operations.append("push1");
mystack.push(originalArray[origindex++]);
char tmpstr[1000];
while( resultindex < length){
if(mystack.empty() && origindex < length){
operations.append("|push");
sprintf(tmpstr,"%d",(origindex+1));
operations.append(tmpstr);
mystack.push(originalArray[origindex++]);
}
if(mystack.top() == resultArray[resultindex]) {
operations.append("|pop");
for(index=0;index
if(mystack.top()==originalArray[index])
break;
}
sprintf(tmpstr,"%d",(index+1));
operations.append(tmpstr);
mystack.pop();
resultindex++;
}
else { if(origindex < length) {
operations.append("|push");
sprintf(tmpstr,"%d",(origindex+1));
operations.append(tmpstr);
mystack.push(originalArray[origindex++]);
}
else {
//top!=result and all elements in original are in stack now
//mismatch now
break;
}
}
}
char *ptr=NULL;
if(! mystack.empty()) {
ptr=new char[10];
strcpy(ptr,"None");
}
else {
ptr=new char[operations.length()+1];
strcpy(ptr,operations.c_str());
}
return ptr;
}
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:
Time Remaining: 00:25:17
1. The push and pop operations deal with the original int array from left to right.
2. The input is two integer array. They are the original array and the result array. These interger array is split by space.
3. The output is the operation sequence.
4. If the original array cannot make to the result array with stack push and pop, The output should be 'None'.
5. The operation "push1" means push the first element of the original array to the stack.
6. 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.
7. 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
#include < iostream >
#include < cstring >
#include < cstdio >
#include < cstdlib >
#include < stack >
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);
if (NULL != operationSequence)
{ // 原来系统提供的代码。这里没有NULL判断
cout<< operationSequence <<endl;
free(operationSequence); // 自己加的
}
else
cout<< "None" <<endl; // 自己加的
}
return 0;
}
//your code is here
//下面才是让写代码的地方,其他的系统已经自动给出。
char* calculateOperationSequence(int* originalArray, int* resultArray, int length)
{
if(NULL==originalArray || NULL==resultArray || length <= 0)
return NULL;
stack mystack;
int resultindex=0,origindex=0;
int index=0;
string operations("");
operations.append("push1");
mystack.push(originalArray[origindex++]);
char tmpstr[1000];
while( resultindex < length){
if(mystack.empty() && origindex < length){
operations.append("|push");
sprintf(tmpstr,"%d",(origindex+1));
operations.append(tmpstr);
mystack.push(originalArray[origindex++]);
}
if(mystack.top() == resultArray[resultindex]) {
operations.append("|pop");
for(index=0;index
if(mystack.top()==originalArray[index])
break;
}
sprintf(tmpstr,"%d",(index+1));
operations.append(tmpstr);
mystack.pop();
resultindex++;
}
else { if(origindex < length) {
operations.append("|push");
sprintf(tmpstr,"%d",(origindex+1));
operations.append(tmpstr);
mystack.push(originalArray[origindex++]);
}
else {
//top!=result and all elements in original are in stack now
//mismatch now
break;
}
}
}
char *ptr=NULL;
if(! mystack.empty()) {
ptr=new char[10];
strcpy(ptr,"None");
}
else {
ptr=new char[operations.length()+1];
strcpy(ptr,operations.c_str());
}
return ptr;
}
相关文章推荐
- 2012 亚马逊在线笔试题目2
- 2014亚马逊在线笔试题目
- 2014年亚马逊在线笔试题目及解法_9_24(字符串编辑距离升级版)
- 2013亚马逊在线笔试题目
- 2014亚马逊在线笔试题目及解决方案(MMChess问题)
- 网易互联网2017在线笔试题目-魔力手环
- 微软2016校园招聘9月在线笔试-题目3 : Fibonacci
- 亚马逊2013的一道在线笔试题
- 百度2012校园招聘机器学习/数据挖掘工程师(北京)笔试题目
- 2012九月3号阿里巴巴笔试题目
- 百度的在线笔试题目【暂无答案】
- 亚马逊在线笔试第三题--(变种的)三叉树的最近公共祖先问题
- [2014亚马逊amazon] 在线笔试题 大于非负整数N的第一个回文数 Symmetric Number
- 2016阿里巴巴c/c++研发工程师在线笔试题(附加题)(题目写的很乱,纯属碎片)
- 2018大疆校招软件岗位笔试题目(求有效工作时间)——未在线验证
- 2017微软秋季校园招聘在线编程笔试 题目2 Composition
- 滴滴出行2015在线笔试题目
- 百度的在线笔试题目[添加了答案]
- 2012百度招聘笔试题目
- 微软201610 在线笔试 题目3 Registration Day