如何优化此计划?

时间:2016-11-25 18:39:09

标签: c++ string loops optimization deque

我正在参加2月的编码比赛,我正在研究去年的问题集。我想要做的任务就是这个。作为输入,我给出一个整数N(最大10,000,000)和一个长度为N的字符串S.该字符串仅包含字母'x','y'和'z'。

这些字母都是'命令'。你从一个只包含字母'A'的单词开始。如果你得到一个命令'x',你会在你的单词后面添加一个'A'。

Command'y'会在单词的前面添加一个'B'。 'y'会翻转这个词。因此,输入N = 3,并且S =“xyz”,将使该单词为“AAB”。 x:添加'A',y:添加'B'和z:翻转整个单词。 整个过程必须在2秒内完成,这对我来说似乎是个问题。 我希望所有这些都是可以理解的......

嗯,解决方案的解释是说一个双端队列是最有效的方法,但我不能低于10秒执行时间。有人可以帮我找到优化此代码的方法。

using namespace std;
int main() {

    int num = 10000000;
    string commands = "";
    bool reversed = false;

    deque<char>word = { 'A' };

    // just for generating the input. The real program would not need this
    for (int i = 0; i < num / 5; i++) {
        commands += "xyzxy'";
    }


    //cin >> num >> commands;


    for (int i = 0; i < num; i++) {

        if (commands.at(i) == 'x') { //If the command is 'x' add an A at the back
            if (!reversed)
                word.push_back('A');
            else // if it the word is reversed, reverse the command
                word.push_front('A');
        }

        else if (commands.at(i) == 'y') { //If the command is 'y', add a 'B' at the front
            if (!reversed)
                word.push_front('B');
            else // if it the word is reversed, reverse the command
                word.push_back('B');
        }
        else if (commands.at(i) == 'z') { // If the command is 'z' set the status to reversed/!reversed
            reversed = !reversed;
        }
    }

    if (reversed)
        reverse(word.begin(), word.end());

    for (int i = 0; i < word.size(); i++) { // print out the answer

        cout << word.at(i);
    }

    system("pause");

        return 0;
    }

谢谢!

3 个答案:

答案 0 :(得分:0)

通常我们希望服务器每秒执行10 ^ 6个命令。

请记住,N = 10,000,000,时间限制为2秒,需要O(n)复杂度。

您可以使用以下技术实现此目的:

使用双链表。

使用布尔变量,例如flag,如果结果字符串从链表的前面开始,则返回true;如果字符串从链表的后面开始,则返回false。

对于输入字符串push_back的每个字符,如果给定字符为x,则push_front如果给定字符为y并更改flag的值。

读取输入结束时,分别打印字符串以标记值。

复杂性:O(n)

答案 1 :(得分:0)

这个技巧有两个部分。

  1. 使用std::deque,优化插入容器的两端。

  2. 请勿翻阅&#39; z&#39;输入。相反,要跟踪多少次&#39; z&#39;已被阅读,并更改添加AB的代码,以便如果次数为奇数,则将该字符添加到该单词的另一端。

  3. 在输入结束时,如果最终计数为奇数,则只需将字符串翻转一次。

    主要技巧是避免浪费时间一遍又一遍地翻转弦乐。你只需要翻转一次。

答案 2 :(得分:0)

观察:矢量上的push_back比deque上的push_back和push_front都要快。

如果你使用两个向量(正面和背面)并将前面视为反转,那么你也可以通过在后向量上使用push_back来消除所有'if(反向)......'的东西x,y的前向量上的push_back,以及当你点击z时交换向量。

然后在输出结果时,通过前向量进行反向迭代,通过后向量进行前向迭代。

var switchedObj = [
  {latitude: lat1, longitude: lng1},
  {latitude: lat2, longitude: lng2}
]

它在我的机器上没有产生积极的影响,但你也可以在开始构建单词之前在前后向量上保留一些空间。