Yodaness没有输出

时间:2018-03-02 23:59:45

标签: c++ output mergesort

我正在尝试解决yodaness问题,但是当我运行程序时,我的代码没有输出任何内容。我不确定我的错误在哪里,因为我的代码正在编译并且运行时没有输出。我正在尝试计算一串字符中的反转次数,但我不确定我是否正确地使用它。任何帮助将不胜感激,谢谢

#include <iostream>
#include <sstream>
#include <limits>
#include <map>     
#include <string>  


using namespace std;

const int MAX = 30000, LEN = 33;
int total = 0;

map<string, int> index_words(const string &line)
{
        map<string, int> result;
        istringstream in(line);

        string word;
        int index = 0;   
        while (in >> word)
        {

if (result.find(word) == result.end())
                        result[word] = index++;
        }
        return result;
}
void merge(int *a, int p, int q, int r) {
        int i, j, k, n1 = (q-p+1), n2 = (r-q);
        int L[n1], R[n2];
        for(i=0;i<n1;i++) L[i] = a[p+i];
        for(j=0;j<n2;j++) R[j] = a[q+j+q];
        for(k=p, i=j=0; k<=r; k++) {
                if(j>=n2 || (i<n1 && L[i] <=R[j])) a[k] = L[i++];
                else {
                        total += n1-i;
                        a[i] = R[j++];
                }
        }
}
void mergesort(int *a, int p, int r) {
        if(p<r) {
                int q = (p+r)/2;
                mergesort(a,p,q);
                mergesort(a,q+1,r);
                merge(a,p,q,r);
                total++;
        }
}
char word[MAX][LEN];
int a[MAX];
int main()
{
        cin >> instances;
        char temp[LEN];
        for (int i = 0; i < instances; ++i)
        {
                int numwords;
                cin >> numwords;            
                cin.ignore(numeric_limits<streamsize>::max(), '\n';
                string yoda_line, english_line;

                getline(std::cin, yoda_line);
                getline(std::cin, english_line);

                auto yoda_idx = index_words(yoda_line);
                auto english_idx = index_words(english_line);

                for(int i = 0; i < instances; i++) a[i] = yoda_idx[word[i]];
                mergesort(a,0, instances-1);
                cout << total;
        }
}

0 个答案:

没有答案