我怎样才能让这个c++编程速度更快?

发布于 2025-01-09 06:16:05 字数 1861 浏览 0 评论 0原文

我正在尝试使用合并排序和标准字符串比较对字符串进行排序。问题是,这个程序本来就很慢。当我尝试检查我的解决方案时,我超出了运行时预期。有没有办法可以使用其他数据结构来做同样的事情,但访问速度更快?这是代码。感谢您的帮助。

编辑:我知道使用 sort() 而不是我自己的合并排序算法会更快,但我的要求是使用合并排序。问题在于超出了 SPOJ 的运行时要求。

#include <iostream>
using namespace std;

void MergeSortA(int low , int high);
void MergeA(int low ,int mid ,int high);

string stringData[50000];
int main()
{
    int testCases;
    int linesInTestCase;

    scanf("%d", &testCases);
    while(testCases--)
    {
        scanf("%d", &linesInTestCase);
        for(int i = 0; i < linesInTestCase; ++i)
            cin >> stringData[i];

        // Sort the data
        MergeSortA(0, linesInTestCase - 1);

        // Print array of strings one by one
        for(int i = 0; i < linesInTestCase; ++i) {
            cout << stringData[i] << endl;
        }
    }
    return 0;
}

void MergeSortA(int low , int high)
{
    int mid = 0;
    if(low < high)
    {
        mid = ((low+high)/2);
        MergeSortA(low , mid);
        MergeSortA(mid+1,high);
        MergeA(low,mid,high);
    }
}
void MergeA(int low ,int mid , int high)
{
    int i = low, j = mid+1 , k = low;
    string Temp[50000];

    while(i <= mid && j <= high) {
        if( stringData[i] < stringData[j] ) {
            Temp[k].assign(stringData[i]);
            i++;
        }
        else {
            Temp[k].assign(stringData[j]);
            j++;
        }
        k++;
    }

    if(i > mid ) {
        for(int h = j ;h <= high ; h++ ) {
            Temp[k].assign(stringData[h]);
            k++;
        }
    }
    else
        for(int h = i; h<= mid ; h++ ) {
            Temp[k].assign(stringData[h]);
            k++;
        }

    // Copy from low to high
    for(int l = low; l <= high ; l++) {
        stringData[l].assign(Temp[l]);
    }
}

I am trying to sort strings using merge sort and standard string comparison. The problem is, this program is very slow as is. I am exceeding runtime expectations when I am attempting to check my solution. Is there a way I can use some other data structure to do the same thing but be accessed faster? Here is the code. Thank you for your help.

Edit: I understand that using sort() rather than my own merge sort algorithm will be faster, but my requirement states to use merge sort. The problem is exceeding runtime requirements on SPOJ.

#include <iostream>
using namespace std;

void MergeSortA(int low , int high);
void MergeA(int low ,int mid ,int high);

string stringData[50000];
int main()
{
    int testCases;
    int linesInTestCase;

    scanf("%d", &testCases);
    while(testCases--)
    {
        scanf("%d", &linesInTestCase);
        for(int i = 0; i < linesInTestCase; ++i)
            cin >> stringData[i];

        // Sort the data
        MergeSortA(0, linesInTestCase - 1);

        // Print array of strings one by one
        for(int i = 0; i < linesInTestCase; ++i) {
            cout << stringData[i] << endl;
        }
    }
    return 0;
}

void MergeSortA(int low , int high)
{
    int mid = 0;
    if(low < high)
    {
        mid = ((low+high)/2);
        MergeSortA(low , mid);
        MergeSortA(mid+1,high);
        MergeA(low,mid,high);
    }
}
void MergeA(int low ,int mid , int high)
{
    int i = low, j = mid+1 , k = low;
    string Temp[50000];

    while(i <= mid && j <= high) {
        if( stringData[i] < stringData[j] ) {
            Temp[k].assign(stringData[i]);
            i++;
        }
        else {
            Temp[k].assign(stringData[j]);
            j++;
        }
        k++;
    }

    if(i > mid ) {
        for(int h = j ;h <= high ; h++ ) {
            Temp[k].assign(stringData[h]);
            k++;
        }
    }
    else
        for(int h = i; h<= mid ; h++ ) {
            Temp[k].assign(stringData[h]);
            k++;
        }

    // Copy from low to high
    for(int l = low; l <= high ; l++) {
        stringData[l].assign(Temp[l]);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愛上了 2025-01-16 06:16:05

正如您所说,这是 SPOJ 问题,我会尝试不改变您的程序结构,但是,这里存在真正的瓶颈:

void MergeA(int low ,int mid , int high)
{
    int i = low, j = mid+1 , k = low;
    string Temp[50000]; //<--- Allocation happens every time this function is called

无论何时调用 MergeA,都会构造 50000 个字符串。每次都会被破坏,阻碍算法。只需添加 static 即可:

void MergeA(int low ,int mid , int high)
{
    int i = low, j = mid+1 , k = low;
    static string Temp[50000]; //Now it is constructed & destructed only once in the program

As you stated that this is SPOJ problem, I will try not alter your structure of your program, However, there is real bottleneck here:

void MergeA(int low ,int mid , int high)
{
    int i = low, j = mid+1 , k = low;
    string Temp[50000]; //<--- Allocation happens every time this function is called

Wnenever MergeA is called, 50000 strings are constructed & destructed every time, encumbering the algorithm. Just add static there:

void MergeA(int low ,int mid , int high)
{
    int i = low, j = mid+1 , k = low;
    static string Temp[50000]; //Now it is constructed & destructed only once in the program
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文