<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Puzzle</title>
</head>

<body>

<blockquote>

<blockquote>
  <blockquote>

<p align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#FF0000" size="6"><b>Code   
Competition(2)&nbsp;</b></font>    
</p>
  </blockquote>
</blockquote>
</blockquote>
<div align="left">
  <pre><b><font color="#ff0000" size="5">A.First Edition</font></b></pre>
</div>
<div align="left">
  <pre><b>This is actually first edition of <a href="CodingProb2.htm">Code Competition2.(Click to see the problem)</a> </b></pre>
</div>
<div align="left">
  <pre><b>1¡£ Basic idea: </b></pre>
</div>
<div align="left">
  <pre><b>This is from last year of &quot;Code Competition&quot; of &quot;Concordia Computer Science Association&quot;(CCSS)</b></pre>
</div>
<div align="left">
  <pre><b>This is the simplest one among 4 problems.(I guess that as I browse the result and most of them only figour out</b></pre>
</div>
<div align="left">
  <pre><b>this one and the winning team also solve the 3rd one.) It is actually a common job in data structure courses. </b></pre>
</div>
<div align="left">
  <pre><b>However, as I seldom use the &quot;fstream&quot;, instead I prefer the basic &quot;FILE&quot;. When I want to speed up by using them</b></pre>
</div>
<div align="left">
  <pre><b>I even make some basic mistakes and have to check MSDN and my reference which takes more time. I finished test </b></pre>
</div>
<div align="left">
  <pre><b>after about 2 hours. I guess I won't become the winning team as I think I am not the kind of coding fast.</b></pre>
</div>
<div align="left">
  <pre><b>2¡£ Program design: </b></pre>
</div>
<div align="left">
  <pre><b>Actually it is quite simple except some common pointer operations. The idea to find the longest substr is just </b></pre>
</div>
<div align="left">
  <pre><b>to find among each two words. To do this, just compare the word from very beginning to each one after it. And so</b></pre>
</div>
<div align="left">
  <pre><b>on, simply a double loop.</b></pre>
</div>
<div align="left">
  <pre><b>3¡£ Major function£º</b></pre>
</div>
<div style="WIDTH: 892px; HEIGHT: 102px" align="left">
  <pre><b>     A. </b>void findSub(char* source, char* dest);</pre>
  <pre><b>	</b>Major function to find commonstring between two words. Actually I declare two pointers pointing both to source and </pre>
  <pre>dest, then move the source pointer until find common char between two words, then both move two pointer to next and see if </pre>
  <pre>they are same char. If yes, count one, if not, restart compare. </pre>
  <pre><b>     B. </b>void inputFile(char * fileName)</pre>
  <pre>	Input file and read in words, then call findsub from beginning to end, the second word is the words after current words</pre>
  <pre>till end.	</pre>
  <pre>	</pre>
  <pre><b>4¡£ Further improvement£º</b></pre>
  <pre><b>	A. This simple program cost me almost one hour for debugging due to a low-level mistake:</b></pre>
  <pre><b>	while ((*pSrc==*pDes)!=NULL)</b></pre>
  <pre><b>	{</b></pre>
  <pre><b>	}</b></pre>
  <pre><b>	</b><b><font color="#FF0000"><i>Will you make such stupid mistake like me???</i></font></b></pre>
  <pre><b><font color="#FF0000"><i>	</i></font>B. Programming and debugging cost me more than 3 hours, which means I cannot win the competition at all.</b></pre>
  <pre><b>	C. I forgot to delete dynamically allocated memory at end of execution.</b></pre>
  <pre>#include &lt;iostream&gt;
#include &lt;fstream&gt;
</pre>
  <pre>using namespace std;</pre>
  <pre>const MaxWordLen = 100;</pre>
  <pre>const MaxWordNum = 4000;</pre>
  <pre>int wordCount = 0;</pre>
  <pre>int subLength = 0;</pre>
  <pre>char buffer[MaxWordLen];//double use when findSub</pre>
  <pre>char* word[MaxWordNum];</pre>
  <pre>void findSub(char* source, char* dest);</pre>
  <pre>void inputFile(char* fileName);</pre>
  <pre>void outputFile(char* fileName);</pre>
  <pre>int main(int argc, char* argv[])
{
	inputFile(argv[1]);
	outputFile(argv[2]);</pre>
  <pre>	return 0;
}</pre>
  <pre>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))&amp;&amp;(*pSrc!=NULL))
		{
			temp[tempLen] = *pSrc;
			pSrc++;
			pDes++;			
			tempLen++;			
		}
		if (tempLen&gt;subLength)
		{
			temp[tempLen] = NULL;
			strcpy(buffer, temp);
			subLength = tempLen;
		}
		ptr++;
	}
}
		
</pre>
  <pre>void inputFile(char * fileName)
{
	ifstream stream;
	char *newStr;
	stream.open(fileName);
	while (!stream.eof())
	{
		stream&gt;&gt;buffer;	
		newStr = (char*)malloc(strlen(buffer) + 1);
		strcpy(newStr, buffer);
		word[wordCount] = newStr;
		wordCount++;
	}
	</pre>
  <pre>	for (int i=0; i&lt; wordCount; i++)
	{
		for (int j= i+1; j&lt; wordCount; j++)
		{
			findSub(word[i], word[j]);
		}
	}
}</pre>
  <pre>void outputFile(char* fileName)
{
	ofstream stream;
	stream.open(fileName);
	stream&lt;&lt;buffer;
}</pre>
</div>
<p><font color="#0000FF">This is input file contents:</font>          


</p>

<p><font color="#FF0000">def abcdefg def abc gh abcd swa abc def abcde ss</font>           


</p>

<p><font color="#0000FF">This is output file contents:</font>          


</p>

<p><font color="#FF0000">abcde</font>          


</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                     
&nbsp;&nbsp;&nbsp; <a href="emptySet.htm">          







                       <img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
<a href="index.htm"><img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a>       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<a href="CodingProb3.htm"><img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</a>          


</p>

</body>

</html>
