2014-06-08 23 views
-1

我遇到了這個問題並正在解決它。問題非常簡單。您將得到PUSH和POP命令的輸入集,並且您需要打印適當堆棧的輸出。在線裁判:創建一堆std :: stacks

Sample Input: 
7 
PUSH 1 100 
PUSH 1 200 
PUSH 2 300 
PUSH 2 400 
POP 2 
POP 1 
POP 2 
Sample Output[Expected]: 
400 
200 
300 

爲了解決這個問題,我提出了下面的想法。然而,我的結果並不像人們期望的那樣。我收到很多垃圾值。你能幫我解決這個問題嗎?我已經使用Java解決了這個問題。但法官沒有認同Java代碼。所以,必須在C++中編寫它。對於這裏缺少的任何想法都非常感激。

#include<iostream> 
#include<vector> 
#include<stack> 
#include<string> 
#include<map> 

int main() 
{ 
    std::map<int,std::stack<int> > indexStackMap; // int->stack 

    int noOfInstructions = 0; 
    scanf("%d", &noOfInstructions); 

    for(int inx = 0; inx < noOfInstructions; ++inx) 
    { 
     char instruction[5]; 
     scanf("%s", &instruction);  

     int stackNumber = 0; 
     scanf("%d", &stackNumber); 

     if(strcmp(instruction, "PUSH") == 0) 
     {    
      int stackValue = 0; 
      scanf("%d", &stackValue); 

      std::map<int,std::stack<int> >::iterator intStackIter = indexStackMap.begin(); 

      if(indexStackMap.find(stackNumber) == indexStackMap.end()) 
      { 
       // Element not in yet!. 
       std::stack<int> *tempStack = new std::stack<int>(); 
       tempStack->push(stackValue); 
       indexStackMap[ stackNumber ] = *tempStack;     
      } 
      else 
      { 
       std::stack<int> & ref = intStackIter->second; 
       ref.push(stackValue); 
      } 
     } 
     else 
     { 
      std::map<int,std::stack<int> >::iterator intStackIter = indexStackMap.find(stackNumber); 
      std::stack<int> & ref = intStackIter->second; 
      ref.pop(); 
      printf("%d\n", ref.top()); 
     } 
    } 
    return 0; 
} 

謝謝, Pavan。

編輯:我的解決方案正常工作。但是從判斷中拋出的內存限制超出了錯誤。 http://pastebin.com/hYuGgzp5

編輯:下面的代碼可以解決這個問題。

#include<iostream> 
#include<vector> 
#include<stack> 
#include<string> 
#include<map> 

int main() 
{ 
    std::map<int,std::stack<int> > indexStackMap; // int->stack 

    int noOfInstructions = 0; 
    scanf("%d", &noOfInstructions); 

    for(int inx = 0; inx < noOfInstructions; ++inx) 
    { 
     char instruction[5]; 
     scanf("%s", &instruction);  

     int stackNumber = 0; 
     scanf("%d", &stackNumber); 

     if(strcmp(instruction, "PUSH") == 0) 
     {    
      int stackValue = 0; 
      scanf("%d", &stackValue); 

      indexStackMap[stackNumber].push(stackValue); 
     } 
     else 
     { 
      std::map<int,std::stack<int> >::iterator intStackIter = indexStackMap.find(stackNumber); 
      std::stack<int> & ref = intStackIter->second; 
      printf("%d\n", ref.top()); 
      ref.pop(); 

     } 
    } 
    return 0; 
} 
+4

這個問題似乎是脫離主題,因爲它屬於http://codereview.stackexchange.com/ – yizzlez

+0

@awesomeyi代碼審查網站不是爲了找到代碼中的錯誤。 – interjay

回答

1

當彈出堆棧,你正在看的top()前值調用stack::pop()。這需要反過來,因爲pop()從堆棧中刪除頂層元素。

由於(不必要的)new而沒有匹配delete,您也有內存泄漏。

順便說一句,推一個新的元素,你只需要:

indexStackMap[stackNumber].push(stackValue); 

,而不是整個if聲明。如果需要,這將在地圖上添加一個新的堆棧。

+0

@internjay謝謝,它做到了。我更新了代碼並作爲對此問題的編輯發佈。但我從未意識到可以解決我的工作的下面一塊。 - > indexStackMap [stackNumber] .push(stackValue); 那麼,STL是否足夠智能處理這種情況呢?我的意思是我們沒有向STL地圖提供堆棧實例。但它能夠理解。 – Pavan

+0

Ofcourse,這個解決方案仍然給我一個內存限制的判斷錯誤,這很好。 – Pavan