1

I have modified the code from my previous question, and now it looks like this:

//#include "stdafx.h"
#include <iostream>
#include <cstring>
#include <chrono>
#include <cassert>

using namespace std;
const int  MAX_SIZE=10000;
const int MAX_STRINGS = 10;
char** strings=new char*[10];
int len;

char* GetLongestCommonSubstring( char* str1, char* str2 );
inline void readNumberSubstrings();
inline const char* getMaxSubstring();

void readNumberSubstrings()
{
    cin >> len;

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

    for(int i=0; i<len;i++)
        strings[i]=new char[MAX_SIZE];

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

 const char* getMaxSubstring()
{
    char *maxSubstring=strings[0];
    auto begin = chrono::high_resolution_clock::now();
    for(int i=1; i < len; i++)
        maxSubstring=GetLongestCommonSubstring(maxSubstring, strings[i]);
    cout << chrono::duration_cast <chrono::milliseconds>       (chrono::high_resolution_clock::now()-begin).count() << endl;
    return maxSubstring;
}

char* GetLongestCommonSubstring( char* string1, char* string2 )
{

    if (strlen(string1)==0 || strlen(string2)==0) cerr << "error!";

    int *x=new int[strlen(string2)+ 1]();
    int *y= new int[strlen(string2)+ 1]();

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

    int max_length = 0;
    int result_index = 0;

    int length;
    int M=strlen(string2) - 1;

    for(int i = strlen(string1) - 1; i >= 0; i--)
    {
        for(int j = M; 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);
    }
    delete[] x;
    delete[] y;
    string1[max_length+result_index]='\0';
    return &(string1[result_index]);
}

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

It's still solving the generalised longest common substring problem, and now it's rather fast. But there's a catch: if a user specifies, say, 3 as a number of strings he's about to enter, and then only actually enters one string, this code waits forever. How do I change that?

Community
  • 1
  • 1
Chiffa
  • 1,486
  • 2
  • 19
  • 38
  • If the user says he will enter 3 strings and then enters only one, it's the user who's buggy not your program. Actually, as a user I'd expect your program to *wait for me* until I return from whatever I went to do (possibly for hours or even days). – syam Sep 20 '13 at 23:17
  • Isn't it? I've found no other way of putting it without going into too much detail. But I'm open to any renaming suggestions. – Chiffa Sep 20 '13 at 23:19
  • @syam, indeed, but I'm supposed to deal with it. I know others have. – Chiffa Sep 20 '13 at 23:20
  • How do you want to detect if the user only entered 3 strings (as opposed to just taking a long time to enter them all)? What do you want to do if the user doesn't input 3 strings? – David Brown Sep 20 '13 at 23:22
  • So, what do you want to do precisely? Have a timeout (the user must input the string in a certain time frame)? Detect the end of the input stream (Ctrl+D on *nix)?... – syam Sep 20 '13 at 23:24
  • I don't know. I want to print something, like an error message. – Chiffa Sep 20 '13 at 23:24
  • Well, let's modify the situation a bit: suppose my program would open a file stream and read from it. It would, then, first read a number of arguments, and then attempt to read the arguments themselves. But if there wouldn't be any, what should it do? – Chiffa Sep 20 '13 at 23:26
  • renamed the question; hope it's better now. – Chiffa Sep 20 '13 at 23:29

1 Answers1

0

If you read from a file and the number of arguments isn't equal to the number of arguments provided, just print a nice, clean error message to the user.

"Expected 7 arguments, received 3:" Print out the arguments you found so the user has an idea of what the program is looking at when it spits out the error.

As for human input, I agree with the comments. The program should wait until the user close it or enters all the needed arguments.

Vulcronos
  • 3,428
  • 3
  • 16
  • 24
  • This may sound really stupid, but how do I change my code then (suppose I do read a file)? – Chiffa Sep 21 '13 at 00:02
  • Depends on the file format. Just do a few google searches on how to read a file in c++. It won't be too bad. You just have to decide in what order you will read the arguments and if your arguments will be separated by a new line, comma, tab, space, etc... – Vulcronos Sep 21 '13 at 00:09