<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 6.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>longtail</title>
<style>
<!--
table.MsoTableGrid
	{border:1.0pt solid windowtext;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.0pt;
	font-family:"Times New Roman";
	}
 li.MsoNormal
	{mso-style-parent:"";
	margin-bottom:.0001pt;
	font-size:12.0pt;
	font-family:"Times New Roman";
	margin-left:0cm; margin-right:0cm; margin-top:0cm}
-->
</style>
</head>

<body rightmargin="1" bottommargin="1" marginwidth="1" marginheight="1">



<p align="left"><span lang="en-ca"><font size="6" color="#FF0000"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
practicing...</b></font></span></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A. <span lang="en-ca">First </span>Edition</font></b></pre>
</div>
<div align="left">
	<pre><span lang="en-ca"><b>Some problems comes from &quot;topcoder&quot; and some are from interview companies.</b></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.<span lang="en-ca"><a name="problem"></a>The problem</span></font></b></pre>
</div>
<div align="left">
  ¡¡</div>
<div align="left">
  <b><font color="#ff0000" size="5"><span lang="en-ca"><a name="explain"></a>C</span>.<span lang="en-ca">The
  </span></font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>idea of 
  program</b></font></span></div>
<div align="left">
  ¡¡</div>
<div align="left">
	<p><span lang="en-ca">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	</span></div>
<div align="left">
  <pre>¡¡</pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
	<pre>
<span lang="en-ca"><font size="3" color="#0000FF">.</font></span>
</pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">E</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Further improvement</b></font></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">F</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>File listing</b></font></span></pre>
</div>
<div align="left">
  <pre><b><span lang="en-ca"><font size="4" color="#FF0000">1.question1</font></span></b></pre>
</div>
<div align="left">
  <pre><b><span lang="en-ca"><font size="4" color="#FF0000">2.question2</font></span></b></pre>
</div>
<div align="left">
  <pre><b><span lang="en-ca"><font size="4" color="#FF0000">3.question3</font></span></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="4" color="#FF0000"><b>4.squaredigits</b></font></span></pre>
</div>
<div align="left">
  <pre>¡¡</pre>
</div>
<div align="left">
  <pre><b><span lang="en-ca"><font size="4" color="#FF0000">filename: question1.cpp</font></span></b></pre>
</div>
<div align="left">
  <pre><font color="#0000FF">Problem 1: findMaxReverse

Create a function using one of the following prototypes:

C++:     int findMaxReverse (const int *array, int length);
            -or-
Java:    int findMaxReverse (int array[]);

This function should return the size of the largest sub-array which also appears in reverse order somewhere in the array.  The array will </font></pre>
	<pre><font color="#0000FF">always have less than 10000 elements.

For example:

findMaxReverse ({4,1,2,3,0,7,3,2,1}) returns 3 (the largest sub-array is {1,2,3})
findMaxReverse ({8,6,1,9,8,1,6}) returns 2 (the largest sub-array is {6,1})</font></pre>
	<pre><span lang="en-ca"><font size="4" color="#FF0000"><b>/////////////////////////////////////////////////</b></font></span></pre>
</div>
<div align="left">
  <pre>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;time.h&gt;



int doFindMaxReverse(const int* array, int length)
{
	int result=0;
	
	for (int i=0; i&lt;length/2; i++)
	{
		if (array[i]!=array[length-i-1])
		{
			break;
		}
		else
		{
			result++;
		}
	}
	return result;
}


int findMaxReverse (const int *array, int length)
{
	int result=1, temp;
	for (int i=0; i&lt;length; i++)
	{
		//shrink the array to find
		for (int j=0; j&lt;length-i; j++)
		{

			temp=doFindMaxReverse(array+i, length-i-j);
			if (temp&gt;result)
			{
				result=temp;
			}
		}
	}
	return result;
}


void question1Test()
{
	int array[9]={4,1,2,3,0,7,3,2,1};
	int length=9;
	//int array[7]={8,6,1,9,8,1,6};
	//int length=7;


	printf(&quot;result=%d\n&quot;, findMaxReverse(array, length));
}

void question1RandomTest()
{
	int*array;
	int length;
	length=rand()%10+6;
	array=new int[length];

	for (int i=0; i&lt;length; i++)
	{
		array[i]=rand()%20;
		printf(&quot;%d,&quot;, array[i]);
	}

	printf(&quot;\nresult=%d\n&quot;, findMaxReverse(array, length));
	delete[] array;
}



int main()
{
	srand(time(0));
	question1Test();
	for (int i=0; i&lt;20; i++)
	{
		question1RandomTest();
	}
	return 0;
}</pre>
  <pre><b><span lang="en-ca"><font size="4" color="#FF0000">filename: question2.cpp</font></span></b></pre>
	<pre><font color="#0000FF">Problem 2: findPath

We have a 2D integer array filled with the values 0 and 1.  0 represents a ground tile and 1 represents a wall.  We can only walk up,</font></pre>
	<pre><font color="#0000FF"> down, right or left on ground tiles.

Create a function to determine whether a path exists from the top-left corner of the array (0, 0) to the bottom-right corner (width-1,</font></pre>
	<pre><font color="#0000FF"> height-1).

C++:     bool findPath (const int *array, int width, int height);
-or-
Java:    boolean findPath (int array[], int width, int height);

Input:
            array ¨C Pointer to the 2D array
            width ¨C Width of the 2D array
            height ¨C Height of the 2D array
Output:
            Return value ¨C true if a path exists, false otherwise

For example, given the array:

001011110
100001100
010000011
110001000
111011100

The function would return true.</font></pre>
	<pre><span lang="en-ca"><font size="4" color="#FF0000"><b>///////////////////////////////</b></font></span></pre>
	<pre>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;



bool* path;


bool doFindPath(const int* array, int width, int height, int x, int y)
{
	//boundary condition
	if (x&lt;0||x&gt;=width||y&lt;0||y&gt;=height)
	{
		return false;
	}
	//successful
	if (x==width-1&amp;&amp;y==height-1)
	{
		return true;
	}

	if (path[y*width+x])
	{
		return false;
	}
	//test current
	if (array[y*width+x]==1)
	{
		return false;
	}
	path[y*width+x]=true;

	//recursive
	if (doFindPath(array, width, height, x+1, y))
	{
		return true;
	}
	if (doFindPath(array, width, height, x-1, y))
	{
		return true;
	}
	if (doFindPath(array, width, height, x, y+1))
	{
		return true;
	}
	if (doFindPath(array, width, height, x, y-1))
	{
		return true;
	}
	//if none successful return false
	return false;
}





bool findPath (const int *array, int width, int height)
{
	//quick test
	if (array[height*width-1]==1)
	{
		return false;
	}
	path=new bool[width*height];
	for (int i=0; i&lt;width*height; i++)
	{
		path[i]=false;
	}
	

	return doFindPath(array, width, height, 0, 0);
}

void printArray(int* array, int width, int height)
{
	for (int i=0; i&lt;height; i++)
	{
		for (int j=0; j&lt;width; j++)
		{
			printf(&quot;%d,&quot;, array[i*width+j]);
		}
		printf(&quot;\n&quot;);
	}
	printf(&quot;\n&quot;);
}


void question2RandomTest()
{
	int* array;
	int width, height;
	width=rand()%10+1;
	height=rand()%10+1;
	array=new int[width*height];
	for (int i=0; i&lt;width*height; i++)
	{
		array[i]=rand()%2;
	}
	if (findPath(array, width, height))
	{
		printf(&quot;yes\n&quot;);
	}
	else
	{
		printf(&quot;no\n&quot;);
	}

	printArray(array, width, height);
	delete[]array;

}



void question2Test()
{
	int array[]=
	{
		0,0,1,0,1,1,1,1,0,
		1,0,0,0,0,1,1,0,0,
		0,1,0,0,0,0,0,1,1,
		1,1,0,0,0,1,0,0,0,
		1,1,1,0,1,1,1,0,0
	};
	int width=9;
	int height=5;
	if (findPath(array, width, height))
	{
		printf(&quot;yes\n&quot;);
	}
	else
	{
		printf(&quot;no\n&quot;);
	}
}






int main()
{
	question2Test();

	for (int i=0; i&lt;30; i++)
	{
		question2RandomTest();
	}

	return 0;
}</pre>
	<pre><b><span lang="en-ca"><font size="4" color="#FF0000">filename: question3.cpp</font></span></b></pre>
	<pre><font color="#0000FF">Problem 3: findMaxSplit

We have an integer array which we¡¯d like to split into a sequence of consecutive sub-arrays.  The sum </font></pre>
	<pre><font color="#0000FF">of the values contained in each sub-array should be the same.

Create a function to find the maximum possible number of such sub-arrays in the given input array.

C++:     int findMaxSplit (const int *array, int length);
            -or-
Java:    int findMaxSplit (int array[]);

The array will always have less than 10000 elements.

For example:

findMaxSplit ({1,2,3})                            returns 2  (sum=3, sub-arrays: {1,2}, {3})
findMaxSplit ({1,2,3,6,12,8,4,3,3,3,3})  returns 4  (sum=12, sub-arrays: {1,2,3,6}, {12}, {8,4}, {3,3,3,3})
findMaxSplit ({1,2,6,30})                       returns 1  (can¡¯t be split)
findMaxSplit ({1,2,3,4,2})                      returns 2  (sum=6, sub-arrays: {1,2,3}, {4,2})</font>
</pre>
	<pre><span lang="en-ca"><font size="4" color="#FF0000"><b>/////////////////////////////////////////</b></font></span></pre>
	<pre>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;


bool doFindMaxSplit(const int* array, int len, int target, int current, int&amp; counter)
{
	if (len==0)
	{
		//successful condition
		return current==0;
	}

	if (current+array[0]==target)
	{
		//find match, recursion
		counter++;
		return doFindMaxSplit(array+1, len-1, target, 0, counter);
	}

	if (current+array[0]&lt;target)
	{
		//continue
		return doFindMaxSplit(array+1, len-1, target, current+array[0], counter);
	}
	//busted
	return false;
}

		

int findMaxSplit (const int *array, int len)
{
	int result=1, target=0;
	int counter;
	if (len&lt;=1)
	{
		return 1;
	}


	for (int i=0; i&lt;len-1; i++)
	{
		counter=1;
		target+=array[i];
		if (doFindMaxSplit(array+i+1, len-i-1, target, 0, counter))
		{
			if (counter&gt;result)
			{
				result=counter;
			}
		}
	}
	return result;
}


void question3Test()
{
	//int array[3]={1,2,3};
	//int length=3;

	//int array[11]={1,2,3,6,12,8,4,3,3,3,3};
	//int length=11;

	//int array[5]={1,2,6,30};
	//int length=5;
	
	int array[5]={1,2,3,4,2};
	int length=5;
                    

	printf(&quot;result=%d\n&quot;, findMaxSplit(array, length));
}


void question3RandomTest()
{
	int* array;
	int length;
	length=rand()%100+1;
	array=new int[length];
	for (int i=0; i&lt;length; i++)
	{
		array[i]=rand()%20;
		printf(&quot;[%d]&quot;, array[i]);
	}

	printf(&quot;\n result=%d\n&quot;, findMaxSplit(array, length));
	delete[] array;

}


void test()
{
	for (int i=0; i&lt;20; i++)
	{
		question3RandomTest();
	}
}


int main()
{
	question3Test();

	return 0;
}</pre>
	<pre>¡¡</pre>
	<pre><b><span lang="en-ca"><font size="4" color="#FF0000">filename: squaredigits.cpp</font></span></b></pre>
	<pre>/*
<font color="#0000FF">Problem Statement
????
***Note:  Please keep programs under 7000 characters in length.  Thank you


Class Name: SquareDigits
Method Name: smallestResult
Parameters: int
Returns: int

Define the function S(x) as the sum of the squares of the digits of x.   
For example: S(3)=3*3=9 and S(230)=2*2+3*3+0*0=13.

Define the set T(x) to be the set of unique numbers that are produced by
repeatedly applying S to x.  That is: S(x), S(S(x)), S(S(S(x))), etc... 
For example, repeatedly applying S to 37:
S(37)=3*3+7*7=58.  
S(58)=5*5+8*8=89.
S(89)=145.
S(145)=42. 
S(42)=20.
S(20)=4.
S(4)=16.
S(16)=37. 
Note this sequence will repeat so we can stop calculating now and: 
T(37)={58,89,145,42,20,4,16,37}.
However, note T(x) may not necessarily contain x.  

Implement a class SquareDigits, which contains a method smallestResult.  The
method takes an int, n, as a parameter and returns the smallest int, x, such
that T(x) contains n.

The method signature is (be sure your method is public): 
int smallestResult(int n); 

TopCoder will ensure n is non-negative and is between 0 and 199 inclusive.

Examples:
If n=0: S(0) = 0, so T(0)={0}, so the method should return 0.

If n=2: T(0) through T(10) do not contain the value 2.  If x=11, however:
S(11)=1*1+1*1=2, so T(11) contains 2, and the method should return 11.

If n=10: T(0) through T(6) do not contain 10.  If x=7:
S(7)=49.
S(49)=97.
S(97)=130.
S(130)=10.
S(10)=1.
and it starts to repeat... 
so T(7) is {49,97,130,10,1}, which contains 10, and the method should return 7.

n=1 -&gt; x=1 
n=19 -&gt; x=133
n=85 -&gt; x=5
n=112 -&gt; x=2666
Definition
????
Class:
SquareDigits
Method:
smallestResult
Parameters:
int
Returns:
int
Method signature:
int smallestResult(int param0)
(be sure your method is public)
????

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this </font></pre>
	<pre><font color="#0000FF">information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
*/</font>


#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;set&gt;
#include &lt;map&gt;

using namespace std;
const int MaxNumber=200;
class SquareDigits
{
typedef set&lt;int&gt; IntSet;
typedef map&lt;int, IntSet&gt; SetMap;
typedef map&lt;int, int&gt; IntMap;

private:
	IntMap squareTable;
	SetMap setTable;
	
	int calcSquare(int number);
	bool calcSet(int number, int current);
	
public:
	int smallestResult(int param);
};

int SquareDigits::smallestResult(int param)
{
	int i=0;
	while (true)
	{
		if (calcSet(i, param))
		{
			break;
		}
		i++;
	}
	return i;
}


bool SquareDigits::calcSet(int number, int target)
{
	IntSet::iterator it;
	IntSet mySet, subSet;
	pair&lt;IntSet::iterator, bool&gt; result;
	SetMap::iterator setMapIt;
	int current=number;
	int squareDigit;
	while (true)
	{
		squareDigit=calcSquare(current);
		if (squareDigit==target)
		{
			return true;
		}
		setMapIt=setTable.find(squareDigit);
		if (setMapIt!=setTable.end())
		{
			subSet=setMapIt-&gt;second;
			for (it=subSet.begin(); it!=subSet.end(); it++)
			{
				mySet.insert(*it);
			}
			break;//we find the subset and can quit
		}
		
		result=mySet.insert(squareDigit);
		if (!result.second)
		{
			//it converges
			break;
		}
		current=squareDigit;
	}
	setTable.insert(make_pair(number, mySet));
	return false;
}
				
		

int SquareDigits::calcSquare(int number)
{
	int modulo, remainder=number;
	int sum=0;
	IntMap::iterator it;
	it=squareTable.find(number);
	if (it!=squareTable.end())
	{
		return it-&gt;second;
	}
	while (remainder!=0)
	{
		modulo=remainder%10;
		sum+=modulo*modulo;
		remainder/=10;
	}
	squareTable.insert(make_pair(number, sum));
	return sum;
}


int main()
{
	int result, input=10;
	SquareDigits mySqr;
	result=mySqr.smallestResult(input);

	printf(&quot;%d=%d\n&quot;, input, result);
	return 0;
}</pre>
	<pre>¡¡</pre>
	<pre><span lang="en-ca"><font size="4" color="#FF0000"><b>///////////////////////////////</b></font></span></pre>
	<pre><span lang="en-ca"><font size="4" color="#FF0000"><b>//the following are something I want to find in case there is a coding test for me. :)</b></font></span></pre>
	<pre>#include &lt;stdio.h&gt;

struct Link
{
	int value;
	Link* next;
};

void reverseLink();

void displayLink(Link* list);

void createLink(Link*&amp; list, int length);

Link* findTail(Link* list);

void swapLink(Link* startParent, Link* endParent)
{
	Link* startSon, *endSon, *temp;
	if (endParent==NULL)
	{
		return;
	}	
	startSon=startParent-&gt;next;
	endSon=endParent-&gt;next;
	startParent-&gt;next=endSon;
	endParent-&gt;next=startSon;
	temp=endSon-&gt;next;
	endSon-&gt;next=startSon-&gt;next;
	startSon-&gt;next=temp;
}

void doReverseLink(Link*&amp; startParent, Link*&amp; endParent)
{
	if (endParent==NULL)
	{
		return;
	}
	
	
	if (endParent-&gt;next-&gt;next==NULL)
	{	
		swapLink(startParent, endParent);
		if (startParent-&gt;next!=endParent)
		{
			startParent=startParent-&gt;next;
		}		
		return;
	}
	doReverseLink(startParent, endParent-&gt;next);

	swapLink(startParent, endParent);
	if (startParent-&gt;next!=endParent)
	{
		startParent=startParent-&gt;next;
	}		
}

void swapDouble(Link* grandPa)
{
	Link* sonNext, *father, *son;
	
	sonNext=grandPa-&gt;next-&gt;next-&gt;next;
	father=grandPa-&gt;next;
	son=father-&gt;next;

	grandPa-&gt;next=son;
	son-&gt;next=father;
	father-&gt;next=sonNext;
}
	

	


void createLink(Link* list, int length)
{
	Link* current=list;
	for (int i=0; i&lt;length; i++)
	{
		current-&gt;next=new Link;
		current-&gt;next-&gt;value=i;
		current=current-&gt;next;
	}
	current-&gt;next=NULL;
}


void displayLink(Link* list)
{
	Link* current=list;
	while (current!=NULL)
	{
		printf(&quot;[%d]&quot;, current-&gt;value);
		current=current-&gt;next;
	}
	printf(&quot;*****************\n&quot;);
}


void reverseLink()
{
	Link head, *start;
	head.next=NULL;
	head.value=-1;
	createLink(&amp;head, 6);
	displayLink(head.next);
	start=&amp;head;
	doReverseLink(start, head.next-&gt;next);
	displayLink(&amp;head);
}




int main()
{

	reverseLink();
	return 0;
}

void reverseList(char* str);


//void doReverse(char* start, char* stop);

void swapPtr(char* first, char* second)
{
	char temp;
	temp=*first;
	*first=*second;
	*second=temp;
}


char* getFence(char* head)
{
	if (*(head+1)=='\0')
	{
		return head;
	}
	else
	{
		return getFence(head+1);
	}
}
//at least length=3
void doReverse(char*&amp; target, char*&amp; head, char*&amp; fence)
{
	if (head==fence)
	{
		return;
	}
	else
	{		
		head++;
		doReverse(target, head, fence);
		if (target&gt;=head)
		{
			return;
		}
		swapPtr(target, head);		
		target++;
		head--;		
	}
}


void reverseList(char* str)
{
	char* fence=NULL, *start=str, *head;
	printf(&quot;before reverse=%s\n&quot;, str);
	fence=getFence(str);
	head=start+1;
	
	doReverse(start, head, fence);
	printf(&quot;after reverse=%s\n&quot;, str);
}

void reverseListDemo()
{
	char buf[80];
	sprintf(buf, &quot;12345&quot;);
	reverseList(buf);
}</pre>
	<pre>¡¡</pre>
</div>
<div align="left">
  <pre>¡¡</pre>
</div>
<pre><span lang="en-ca">	</span> <a href="game24.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; <img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35"> </pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<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;</p>

</body>

</html>