2013-04-17 21 views
1

的話如何解決這個問題? https://code.google.com/codejam/contest/351101/dashboard#s=p1反向C++中的句子,要求谷歌代碼果醬

我結束了的代碼如下,但它只能反向串了一個空間,因爲它是代碼保持字面邏輯考慮,反向整個字符串,扭轉的話,和完成。空間稍微混亂了,當我嘗試一個循環來檢測空間數量並據此採取行動時,它失敗了。請幫忙!代碼:

#include <iostream> 
#include <string> 

using namespace std; 


int main() 
{ 

    char revwrd[100]; 
    char revstr[100]; 
    string str; 
    getline(cin, str); 
    cout<<str; 
    int sps[10]; 

    int len,y=0; 
    len = str.length(); 
    cout<<"\n"<<"The Length of the string is:"<<len; 
    for(int x=len-1;x>-1;x--) 
    { 
     revstr[x] = str[y]; 
     y++; 

    } 
    cout<<"\n"<<"The inverse of the string is:"<<"\n"; 
    for(int z = 0;z<len;z++) 
    { 
     cout<<revstr[z]; 
    } 
    cout<<"\n"; 

    int no=0; 
    int spaces=0; 
    for(int a=0;a<len;a++) 
    { 
     if(revstr[a]== ' ') 
     { 
      sps[no]=a; 
      no++; 
      spaces++; 
     } 
    } 

    int rinc=0; 
    int spinc; 
    cout<<"\n"; 
    spinc=sps[0]; 

    int spinc2 = sps[0]+1; 
    int lend; 
    for(rinc=0;rinc<sps[0]+1;rinc++) 
    { 

     revwrd[rinc] = revstr[spinc]; 
     spinc--; 
    } 


    for(lend=len;lend>sps[0];lend--) 
    { 
     revwrd[spinc2] = revstr[lend]; 
     spinc2++; 
    } 
    cout<<"Spaces in the string:"<<spaces<<"\n"; 
    cout<<"The words inversed are:"<<"\n"; 
    for(int inc=1;inc<len+1;inc++) 
    { 
     cout<<revwrd[inc]; 
    } 

    return 0; 
} 
+0

爲什麼你會扭轉整條生產線,然後再重新逆轉的話嗎?在問題中給出的約束條件下,你可以** 1得到該行並將其放入'stringstream'中,** 2 **讀取該行中的每個單詞(只需使用'operator >>(ostream&,string& )'並將它壓入堆棧,** 3 **彈出堆棧中的每個單詞並將其打印到結果中。 (可能有更快的方法,但是這樣做的工作,應該很容易理解。) –

+0

可能有用的三件事:字符串流,向量和反向迭代器。 –

+0

請幫忙!不是一個問題。你有真正的問題嗎? – svick

回答

0

這可能處理多空間:

std::string ReverseSentence(std::string in) 
{ 
    std::vector<string> words; 
    std::string temp = ""; 
    bool isSpace = false; 
    for(int i=0; in.size(); i++) 
    { 
     if(in[i]!=' ') 
     { 
     if(isSpace) 
     { 
      words.push_back(temp); 
      temp = ""; 
      isSpace = false; 
     } 
     temp+=in[i]; 
     } 
     else 
     { 
     if(!isSpace) 
     { 
      words.push_back(temp); 
      temp = ""; 
      isSpace = true; 
     } 
     temp += " "; 
     } 
    } 
    std::reverse(words.begin(),words.end()); 
    std::string out = ""; 
    for(int i=0; i<words.size(); i++) 
    { 
     out+=words[i]; 
    } 
return out; 
} 
0

你可以按照這個方法:

第1步:只是檢查空格輸入數組店的索引號的整數數組。

第2步:現在通過這個整數數組遍歷從最終

step a : make a string by copying characters from this index to previous index . 
     note : since for first element there is no previous element in that case you will copy from this index to end of the input string . 

step b : step a will give you a word from end of input string now add these word with a space to make your output string . 

我希望這會幫助你。

1

挑戰的條件是隻存在單詞之間一個空格,而空格沒有出現在開頭或行的結尾,所以對於這個特殊的鍛鍊你不必擔心保持間距;只要你在每個單詞之間輸入一個單獨的空格,你就很好。

考慮到這一點,你可以使用常規格式的輸入讀每個詞:

std::string word; 
... 
while (stream >> word) 
    // do something with word 

您不必擔心緩衝區的大小,你不必擔心檢測空格等您擔心檢測換行符,但是這很容易使用peek方法來完成:

while (stream >> word) 
{ 
    // do something with word; 
    if (stream.peek() == '\n') 
    break; 
} 

上述循環將讀取從輸入流中各個單詞stream unti它看到一個換行符(有可能是一個更好的方法來做到這一點,但它的工作原理)。現在

,爲了扭轉輸入的每一行,你顯然需要在您閱讀存儲串的地方。最容易做的事情是將它們存儲到一個向量:

std::vector<std::string> strings; 
... 
while (stream >> word) 
{ 
    strings.push_back(word); 
    if (stream.peek() == '\n') 
    break; 
} 

所以,現在你有一個包含在該行的所有字符串的載體,你只需要打印出來以相反的順序。您可以使用反向迭代通過矢量走路:

std::vector<std::string>::reverse_iterator it; 
for (it = strings.rbegin(); it != strings.rend(); ++it) 
{ 
    std::cout << *it << " "; 
} 
std::cout << std::endl; 

rbegin()方法返回一個指向向量中的最後元素的迭代器;該rend()方法返回向量的第一個元素之前指向的元素一個迭代; ++it提前迭代器指向向量中的下一個項目,回到前面; *it給出了迭代器指向的字符串。你可以得到多一點的深奧和使用copy模板函數:

std::copy(strings.rbegin(), 
      strings.rend(), 
      std::ostream_iterator<std::string>(std::cout, " ") 
     ); 

這一個方法調用替換上面的循環。它會創建一個新的ostream_iterator,它會將字符串寫入cout,並由一個空格字符分隔。

對於這個特定練習的條件,這是綽綽有餘的。如果你被要求保持間距,或者說出標點符號或大寫字母,那麼你就必須做一些低級別的事情。

0

這個問題真的遞歸做:

void reverse() 
{ 
    string str; 
    cin >> str; 
    if (cin.peek() != '\n' || cin.eof()) { 
     str = " " + str; 
     reverse(); 
    } 
    cout << str; 
} 

int main(int argc, const char * argv[]) 
{ 
    int count = 0; 
    cin >> count; 
    for (int i = 0; i < count; i++) { 
     cout << "Case #" << (i + 1) << ": "; 
     reverse(); 
     cout << endl; 
    } 
    return 0; 
} 

所以我用字讀字和詞的前面加一個空格,直到行或文件的末尾。一旦到達行尾,遞歸解包並按相反順序打印讀取的字符串。

1

只是幾個循環和if的:

// Reverse Words 
#include <iostream> 
#include <string> 

using namespace std; 

int main() { 
    int tc; cin >> tc; cin.get(); 
    for(int t = 0; t < tc; t++) { 
     string s, k; getline(cin, s); 
     for(int i = (s.length()- 1); i >= 0; i--) { 
      if(s[i] == ' ' || (i == 0)) { 
        if(i == 0) k += ' '; 
       for(int j = i; j < s.length(); j++) { 
        k += s[j]; 
        if(s[j+1] == ' ') break; 
       } 
      } 
     } 
     cout << "Case #" << t + 1 << " " << k << endl; 
    } 

    return 0; 
} 
+0

好工作。雖然我不知道你爲什麼決定使用tc(一些案例),一個案例應該足夠了。 – moldovean

0

我去蠻力,我巴不得使用指針!

  1. 得到一句
  2. 檢測的每一個字,並把它們放在一個容器中。
  3. 向後讀取容器。

這是它:

#include <iostream> 
#include <string> 
#include <vector> 
int main() 
{ 
char *s1 = new char[100]; 
std::cin.getline(s1, 100); 

std::vector<std::string> container; 
char* temp = new char[100]; 
char *p1, *p0; 

p1 =p0 = s1; 
int i; 
do{ 
    if (*p1==' ' || *p1=='\0'){ 
     //std::cout<<p1-p0<<' '; 
     for(i=0;i<p1-p0;++i) temp[i]=p0[i]; temp[i]='\0'; 
     p0 = p1+1; 
     container.push_back(temp); 
     std::cout<<temp; 
    } 
    p1++; 
}while(*(p1-1)!='\0'); 

std::cout<<std::endl; 
for(int i=container.size()-1;i>=0;i--) std::cout<<container[i]<<' '; 

return 0; 
}