Puzzle

> > > Code Competition(2)

    A.First Edition

    This is actually first edition of Code Competition2.(Click to see the problem)

    1¡£ Basic idea:

    This is from last year of "Code Competition" of "Concordia Computer Science Association"(CCSS)

    This is the simplest one among 4 problems.(I guess that as I browse the result and most of them only figour out

    this one and the winning team also solve the 3rd one.) It is actually a common job in data structure courses.

    However, as I seldom use the "fstream", instead I prefer the basic "FILE". When I want to speed up by using them

    I even make some basic mistakes and have to check MSDN and my reference which takes more time. I finished test

    after about 2 hours. I guess I won't become the winning team as I think I am not the kind of coding fast.

    2¡£ Program design:

    Actually it is quite simple except some common pointer operations. The idea to find the longest substr is just

    to find among each two words. To do this, just compare the word from very beginning to each one after it. And so

    on, simply a double loop.

    3¡£ Major function£º

         A. void findSub(char* source, char* dest);

    	Major function to find commonstring between two words. Actually I declare two pointers pointing both to source and

    dest, then move the source pointer until find common char between two words, then both move two pointer to next and see if

    they are same char. If yes, count one, if not, restart compare.

         B. void inputFile(char * fileName)

    	Input file and read in words, then call findsub from beginning to end, the second word is the words after current words

    till end.

    4¡£ Further improvement£º

    	A. This simple program cost me almost one hour for debugging due to a low-level mistake:

    	while ((*pSrc==*pDes)!=NULL)

    	{

    	}

    	Will you make such stupid mistake like me???

    	B. Programming and debugging cost me more than 3 hours, which means I cannot win the competition at all.

    	C. I forgot to delete dynamically allocated memory at end of execution.

    #include <iostream>
    #include <fstream>

    using namespace std;

    const MaxWordLen = 100;

    const MaxWordNum = 4000;

    int wordCount = 0;

    int subLength = 0;

    char buffer[MaxWordLen];//double use when findSub

    char* word[MaxWordNum];

    void findSub(char* source, char* dest);

    void inputFile(char* fileName);

    void outputFile(char* fileName);

    int main(int argc, char* argv[])
    {
    	inputFile(argv[1]);
    	outputFile(argv[2]);

    	return 0;
    }

    void findSub(char* source, char* dest)
    {
    	char* ptr = source;
    	char* pSrc= source;
    	char* pDes = dest;
    	char temp[100];
    	int tempLen =0;
    	while (*ptr!= NULL)
    	{
    		tempLen =0;
    		pSrc = ptr;
    		pDes = dest;
    		while((toupper(*pSrc)==toupper(*pDes))&&(*pSrc!=NULL))
    		{
    			temp[tempLen] = *pSrc;
    			pSrc++;
    			pDes++;
    			tempLen++;
    		}
    		if (tempLen>subLength)
    		{
    			temp[tempLen] = NULL;
    			strcpy(buffer, temp);
    			subLength = tempLen;
    		}
    		ptr++;
    	}
    }

    void inputFile(char * fileName)
    {
    	ifstream stream;
    	char *newStr;
    	stream.open(fileName);
    	while (!stream.eof())
    	{
    		stream>>buffer;
    		newStr = (char*)malloc(strlen(buffer) + 1);
    		strcpy(newStr, buffer);
    		word[wordCount] = newStr;
    		wordCount++;
    	}

    	for (int i=0; i< wordCount; i++)
    	{
    		for (int j= i+1; j< wordCount; j++)
    		{
    			findSub(word[i], word[j]);
    		}
    	}
    }

    void outputFile(char* fileName)
    {
    	ofstream stream;
    	stream.open(fileName);
    	stream<<buffer;
    }

This is input file contents:

def abcdefg def abc gh abcd swa abc def abcde ss

This is output file contents:

abcde