1

我的代码在 MS VS 2012 中编译后,按应有的方式接受输入,然后崩溃并显示以下报告:

Debug Assertion Failed!
...\include\xstring
Line:1662
Expression:string subscript out of range

代码如下:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <cassert>
#include <time.h>


using namespace std;


const unsigned short MAX_STRINGS = 10;
const unsigned int  MAX_SIZE=10000;
vector<string> strings;
unsigned int len;

string GetLongestCommonSubstring( string string1, string string2 );
inline void readNumberSubstrings();
inline const string getMaxSubstring();

void readNumberSubstrings()
{
    cin >> len;

    assert(len > 1 && len <=MAX_STRINGS);

    strings.resize(len);

    for(unsigned int i=0; i<len;i++)
        strings[i]=string(MAX_SIZE,0);

    for(unsigned int i=0; i<len; i++)
        cin>>strings[i];
}

 const string getMaxSubstring()
{
    string maxSubstring=strings[0];
    long T=clock();
    for(unsigned int i=1; i < len; i++)
        maxSubstring=GetLongestCommonSubstring(maxSubstring, strings[i]);
    cout << clock()-T << endl;
    return maxSubstring;
}

string GetLongestCommonSubstring( string string1, string string2 ) 
{

    const int solution_size = string2.length()+ 1;

    int *x=new int[solution_size]();
    int *y= new int[solution_size]();

    int **previous = &x;
    int **current = &y;

    unsigned int max_length = 0;
    unsigned int result_index = 0;

    unsigned int j;
    unsigned int length;
    int J=string2.length() - 1;

    for(unsigned int i = string1.length() - 1; i >= 0; i--)
    {
        for(j = J; j >= 0; j--) 
        {
            if(string1[i] != string2[j]) 
                (*current)[j] = 0;
            else 
            {
                length = 1 + (*previous)[j + 1];
                if (length > max_length)
                {
                    max_length = length;
                    result_index = i;
                }

                (*current)[j] = length;
            }
        }

        swap(previous, current);
    }
    string1[max_length+result_index]='\0';
    return &(string1[result_index]);
}

int main()
{
    readNumberSubstrings();
    cout << getMaxSubstring() << endl;
    return 0;
}

我想它一定是一些非常简单的东西(比如一些循环条件或类似的东西)导致它崩溃,但我看不到它。

4

2 回答 2

7

您的问题在于使用unsigned int. 看看这一行:

for(j = J; j >= 0; j--)

这里,在 j 变为 0 后,它会递减。但是你的jis unsigned,所以不是成为-1(并在下一次迭代中退出循环),而是j成为4158584613,并且循环继续,试图访问4158584613该行索引处明显的键外元素:

(*current)[j] = 0;

制作i,jlength签名将解决您的问题。

于 2013-09-19T07:25:40.337 回答
0

我将它加载到本地 Visual Studio 2008 上的测试解决方案中,并将 unsigned int 替换为 i 和 j 中的 int 并运行

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <cassert>
#include <time.h>


using namespace std;


const unsigned short MAX_STRINGS = 10;
const unsigned int  MAX_SIZE=10000;
vector<string> strings;
unsigned int len;

string GetLongestCommonSubstring( string string1, string string2 );
inline void readNumberSubstrings();
inline const string getMaxSubstring();

void readNumberSubstrings()
{
    cin >> len;

    assert(len > 1 && len <=MAX_STRINGS);

    strings.resize(len);

    for(unsigned int i=0; i<len;i++)
        strings[i]=string(MAX_SIZE,0);

    for(unsigned int i=0; i<len; i++)
        cin>>strings[i];
}

 const string getMaxSubstring()
{
    string maxSubstring=strings[0];
    long T=clock();
    for(unsigned int i=1; i < len; i++)
        maxSubstring=GetLongestCommonSubstring(maxSubstring, strings[i]);
    cout << clock()-T << endl;
    return maxSubstring;
}

string GetLongestCommonSubstring( string string1, string string2 ) 
{

    const int solution_size = string2.length()+ 1;

    int *x=new int[solution_size]();
    int *y= new int[solution_size]();

    int **previous = &x;
    int **current = &y;

    unsigned int max_length = 0;
    unsigned int result_index = 0;

    int j;
    unsigned int length;
    int J=string2.length() - 1;

    for(int i = string1.length() - 1; i >= 0; i--)
    {
        for(j = J; j >= 0; j--) 
        {
            if(string1[i] != string2[j]) 
                (*current)[j] = 0;
            else 
            {
                length = 1 + (*previous)[j + 1];
                if (length > max_length)
                {
                    max_length = length;
                    result_index = i;
                }

                (*current)[j] = length;
            }
        }

        swap(previous, current);
    }
    string1[max_length+result_index]='\0';
    return &(string1[result_index]);
}

int main()
{
    readNumberSubstrings();
    cout << getMaxSubstring() << endl;
    return 0;
}

谢谢尼拉吉·拉蒂

于 2013-09-19T07:23:31.207 回答