3

I am trying to practice C++ by doing some old Google Code Jam problems. A relatively simple one I found is to reverse the words in a string. It can be found here https://code.google.com/codejam/contest/351101/dashboard#s=p1

So far I have:

#include<iostream>
using namespace std;

int main(){
    int n = 0;
    cin >> n;


    string rev = "";
    string buf = "";

    string data = "";
    getline(cin, data);

    for(int _ = 0; _ < n; _++){
        getline(cin, data);

        rev = "";
        buf = "";
        for(char& c : data) {
            buf += c;
            if(c == ' '){
                rev = buf + rev;
                buf = "";
            }
        }

        cout << "Case #" << _ + 1 << ": " << buf << " " << rev << endl;
    }

    return 0;
}

Which seems to run pretty fast. When running time ./reverse < in > /dev/null with a test file of around 1.2E6 cases it takes around 3.5 seconds when compiled with g++ -O3.

So as a benchmark I created a solution in python

#!/usr/bin/env python
from sys import stdin, stdout
stdout.writelines(map(lambda n: "Case #%d: %s\n" % (n + 1, ' '.join(stdin.readline().split()[::-1])), xrange(int(stdin.readline()))))

However when I run it under pypy with time pypy reverse.py < in > /dev/null it takes only about 1.95 seconds.

In theory as pypy is written in C++ shouldn't C++ be as fast or faster and if so how could this code be optimised to be faster ?

4

3 回答 3

1

一种简单的非复制/非分配标记器是可恶的std::strtok

以下在我的测试中击败了您的 python 程序

#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>
#include <cstring>

int main()
{
    std::cout.sync_with_stdio(false); // we don't need C in the picture

    std::string line;
    getline(std::cin, line);
    int num_cases = stoi(line);

    std::vector<char*> words;
    for(int n = 0; getline(std::cin, line) && n < num_cases; ++n)
    {   
        words.clear();
        char* p = std::strtok(&line[0], " ");
        while (p) {
            words.push_back(p);
            p = std::strtok(nullptr, " ");
        }
        std::cout << "Case #" << n + 1 << ": ";
        reverse_copy(words.begin(), words.end(),
                     std::ostream_iterator<char*>(std::cout, " "));
        std::cout << '\n'; // never std::endl!
    }
}   

PS:您的 C++ 和 python 输出不完全匹配;这个程序匹配你的 C++ 输出

于 2013-07-03T03:33:20.003 回答
1

我认为当您连接字符串时,您的 C++ 代码正在执行相当多的内存副本(std::string 的大多数实现将整个字符串保持在内存中连续。)我认为以下代码在没有副本的情况下执行此操作,但我没有对其进行太多测试. 至于为什么python表现得很好,我不完全确定。

#include<iostream>

int main()
{
    size_t numCases;
    std::cin >> numCases;
    std::cin.ignore();

    for( size_t currentCase = 1; currentCase <= numCases; ++currentCase )
    {
        std::cout << "Case #" << currentCase << ": ";

        std::string line;
        getline(std::cin, line);
        size_t wordEnd = line.length() - 1;
        size_t lastSpace = std::string::npos;
        for ( int pos = wordEnd - 1; pos >= 0; --pos )
        {
            if ( line[pos] == ' ' )
            {
                for ( int prt = pos + 1; prt <= wordEnd; ++prt )
                    std::cout << line[prt];
                std::cout << ' ';
                lastSpace = pos;
                wordEnd = pos - 1;
                --pos;
            }
        }
        for ( int prt = 0; prt < lastSpace; ++prt )
            std::cout << line[prt];

        std::cout << std::endl;
    }

    return 0;
}
于 2013-07-02T22:01:24.653 回答
-2

您可以利用算法和迭代器库来更简单地完成这一切,而不是使用两个缓冲区和大量连接。我不确定它会快多少(虽然我猜是但是),但它也更紧凑。

#include<iostream>
#include<algorithm>
#include<iterator>
#include<sstream>
using namespace std;

int main(){
    int n = 0;
    cin >> n;
    string data = "";
    getline(cin, data);
    for(int _ = 0; _ < n; _++){
        getline(cin, data);
        stringstream ss(data);
        reverse(istream_iterator<string>(ss), istream_iterator<string>());
        cout << "Case #" << _ + 1 << ": " << ss.str() << endl;
    }
    return 0;
}
于 2013-07-02T22:02:17.903 回答