<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>How Easy?</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; 
How Easy?</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>This is the problem from topCoder competition and it is only a small fun for me. I know I am not good at this kind </b></span></pre>
</div>
<div align="left">
	<pre><span lang="en-ca"><b>of competition. Anyway it is no harm to have some fun.</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">
  　<table id="table1">
	<tr>
		<td colspan="2">
		<h3>Problem Statement </h3>
		</td>
	</tr>
	<tr>
		<td>&nbsp;&nbsp;&nbsp;&nbsp;
        </td>
		<td>
		<pre>***Note:  Please keep programs under 7000 characters in length.  Thank you


Class Name: HowEasy
Method Name: pointVal
Parameters: String
Returns: int
 
TopCoder has decided to automate the process of assigning problem difficulty
levels to problems.  TopCoder developers have concluded that problem difficulty
is related only to the Average Word Length of Words in the problem statement:

If the Average Word Length is less than or equal to 3,  the problem is a 250
point problem.
If the Average Word Length is equal to 4 or 5, the problem is a 500 point
problem.
If the Average Word Length is greater than or equal to 6, the problem is a 1000
point problem.
 
Definitions:
Token - a set of characters bound on either side by spaces, the beginning of
the input String parameter or the end of the input String parameter.
Word - a Token that contains only letters (a-z or A-Z) and may end with a
single period. A Word must have at least one letter.
Word Length - the number of letters in a Word. (NOTE: a period is NOT a letter)

The following are Words :
&quot;ab&quot;,  &quot;ab.&quot;

The following are not Words :
&quot;ab..&quot;, &quot;a.b&quot;, &quot;.ab&quot;, &quot;a.b.&quot;, &quot;a2b.&quot;, &quot;.&quot;

Average Word Length - the sum of the Word Lengths of every Word in the problem
statement divided by the number of Words in the problem statement.  The
division is integer division. If the number of Words is 0, the Average Word
Length is 0.
 
Implement a class HowEasy, which contains a method pointVal.  The method takes
a String as a parameter that is the problem statement and returns an int that
is the point value of the problem (250, 500, or 1000). The problem statement
should be processed from left to right.
 
Here is the method signature (be sure your method is public):
int pointVal(String problemStatement);
 
problemStatement is a String containing between 1 and 50 letters, numbers,
spaces, or periods.  TopCoder will ensure the input is valid.
 
Examples:
 
If problemStatement=&quot;This is a problem statement&quot;, the Average Word Length is
23/5=4, so the method should return 500.
If problemStatement=&quot;523hi.&quot;, there are no Words, so the Average Word Length is
0, and the method should return 250.
If problemStatement=&quot;Implement a class H5 which contains some method.&quot; the
Average Word Length is 38/7=5 and the method should return 500.
If problemStatement=&quot; no9 . wor7ds he8re. hj..&quot; the Average Word Length is 0,
and the method should return 250.</pre>
		</td>
	</tr>
	<tr>
		<td colspan="2">
		<h3>Definition </h3>
		</td>
	</tr>
	<tr>
		<td>&nbsp;&nbsp;&nbsp;&nbsp;
        </td>
		<td>
		<table id="table2">
			<tr>
				<td>Class:
              	</td>
				<td>HowEasy
              	</td>
			</tr>
			<tr>
				<td>Method:
              	</td>
				<td>pointVal
              	</td>
			</tr>
			<tr>
				<td>Parameters:
              	</td>
				<td>string
              	</td>
			</tr>
			<tr>
				<td>Returns:
              	</td>
				<td>int
              	</td>
			</tr>
			<tr>
				<td>Method signature:
              	</td>
				<td>int pointVal(string param0)
              	</td>
			</tr>
			<tr>
				<td colspan="2">(be sure your method is public)
              	</td>
			</tr>
		</table>
		</td>
	</tr>
	<tr>
		<td>&nbsp;&nbsp;&nbsp;&nbsp;
        </td>
	</tr>
	<tr>
		<td>　</td>
	</tr>
	</table>
	<p>This problem statement is the exclusive and proprietary property of 
	TopCoder, Inc. Any unauthorized use or reproduction of this information 
	without the prior written consent of TopCoder, Inc. is strictly prohibited. 
	(c)2003, TopCoder, Inc. All rights reserved. </p>
　</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">
  <span lang="en-ca">My idea is simply drawing a NFA with following states:</span></div>
<div align="left">
  　</div>
<div align="left">
  <span lang="en-ca">StartState, WordState, PeriodState, TokenState, EndState, 
	CountWordState, CountWordEndState</span></div>
<div align="left">
  　</div>
<div align="left">
  　<p><span lang="en-ca">Please note that states like CountWordState and 
	CountWordEndState are connected with StartState with Epsilon.</span></p>
	<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;&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>There <span lang="en-ca">is nothing special for this program except that I was given a mark of 80% and I don't know where is the bug.</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>#include &lt;iostream&gt;

using namespace std;


//An NFA state
enum MyState
{
	StartState, WordState, PeriodState, TokenState, EndState, CountWordState, 
		CountWordEndState
};

//char type
enum MyCharType
{
	AlphaType, PeriodType, SpaceType, OtherType, EndType
};

class HowEasy
{

private:
	MyCharType getCharType(char ch);
	MyState stateMachine(MyState oldState, MyCharType myCharType);
	int countWord(int wordCounter, int lengthSum);

public:
	int pointVal(string param0);
};

//assume input is valid
MyCharType HowEasy::getCharType(char ch)
{
	if (ch&gt;='a'&amp;&amp;ch&lt;='z'||ch&gt;='A'&amp;&amp;ch&lt;='Z')
	{
		return AlphaType;
	}
	if (ch==' ')
	{
		return SpaceType;
	}
	if (ch=='.')
	{
		return PeriodType;
	}

	//EndType is virtual and logical
	/*
	if (ch=='\0')
	{
		return EndType;
	}
	*/
	
	return OtherType;
}

int HowEasy::countWord(int wordCounter, int lengthSum)
{
	int result;
	if (wordCounter==0)
	{
		result= 0;
	}
	else
	{
		result= lengthSum/wordCounter;
	}

	if (result&lt;=3)
	{
		return 250;
	}
	else
	{
		if (result==4||result==5)
		{
			return 500;
		}
		else
		{
			return 1000;
		}
	}
}

int HowEasy::pointVal(string param0)
{
	MyState myState=StartState;
	int lengthSum=0;
	int wordCounter=0;
	int lengthCounter=0;
	MyCharType myCharType;
	
	char ch;
	int strLengthPlusOne=param0.length()+1;
	for (int i=0; i&lt;strLengthPlusOne; i++)
	{
		if (i!=strLengthPlusOne-1)
		{
			ch=param0[i];
			myCharType=getCharType(ch);
		}
		else
		{
			myCharType=EndType;
		}
		myState=stateMachine(myState, myCharType);
		switch (myState)
		{
		case EndState:			
			break;
		case CountWordState:
			lengthSum+=lengthCounter;
			wordCounter++;
			lengthCounter=0;
			//must retore state as this is NFA not DFA
			myState=StartState;
			break;
		case CountWordEndState:
			lengthSum+=lengthCounter;
			wordCounter++;
			break;			
		case WordState:
			lengthCounter++;
			break;
		case PeriodState:
			
			break;
		default:
			lengthCounter=0;
			break;
		}		
	}
	return countWord(wordCounter, lengthSum);
}

MyState HowEasy::stateMachine(MyState oldState, MyCharType myCharType)
{
	MyState result;
	switch (oldState)
	{
	case StartState:		
		switch (myCharType)
		{
		case AlphaType:
			result= WordState;
			break;
			
		case SpaceType:
			result= StartState;
			break;
		case EndType:
			result= EndState;
			break;
		default:
			result= TokenState;	
			break;
		}
		break;

	case WordState:
		switch (myCharType)
		{
		case AlphaType:
			result= WordState;
			break;
		case SpaceType:		
			result= CountWordState;
			break;

		case EndType:
			result= CountWordEndState;
			break;
			
		case PeriodType:
			result= PeriodState;
			break;
		
		default:
			result= TokenState;
			break;
		}
		break;		
	case TokenState:
		switch (myCharType)
		{
		case SpaceType:
			result= StartState;
			break;
		case EndState:
			result= EndState;
			break;
		default:
			result= TokenState;
			break;
		}
		break;
	case PeriodState:
		switch (myCharType)
		{
		case SpaceType:		
			result= CountWordState;
			break;

		case EndType:
			result= CountWordEndState;
			break;
		default:
			result= TokenState;
			break;
		}
		break;
	}
	return result;
}

int main()
{
	HowEasy test;
	cout&lt;&lt;test.pointVal(string(&quot; no9 . wor7ds he8re. hj..&quot;))&lt;&lt;endl;

	return 0;
}

			

<span lang="en-ca">///////////////////////////////////////////////////////////////////////////////////////</span></pre>
	<pre><b><font color="#FF0000">/////////////////////////////////////////////////////////////////////</font></b></pre>
	<pre><font color="#FF0000"><b>//suppose you write code after several months, would you make the exact same choice as before?</b></font></pre>
	<pre><font color="#FF0000"><b>//The following may reveal how I am thinking after one month.</b></font></pre>
	<pre><font color="#FF0000"><b>//////////////////////////////////////////////////</b></font></pre>
	<pre>　</pre>
	<pre><span lang="en-ca">//////////////////////////////////////////////////////////////////////////////////////</span></pre>
	<pre><span lang="en-ca">//second try</span></pre>
	<pre>　</pre>
	<pre>#include &lt;stdio.h&gt;
#include &lt;string&gt;
using namespace std;
enum State
{
	StartString, BeforeWord, InsideWord, NoneWord, PeriodWord, EndString, ErrorState
};

enum CharSet
{
	Alphabetic, Period, Space, Null, Others 
};

class HowEasy
{
private:
	int total;
	int current;
	int wordCounter;
	State automata(CharSet ch, State state); 
	bool isAlpha(char ch);
	CharSet whatChar(char ch);
public:
	int pointVal(string param);
};

int HowEasy::pointVal(string param)
{
	State state=StartString;
	CharSet charSet;
	int average;
	int i=0;
	const char* ptr=param.c_str();
	while (state!=EndString)
	{
		charSet=whatChar(ptr[i]);
		state=automata(charSet, state);
		i++;
	}
	if (wordCounter!=0)
	{
		average=total/wordCounter;
	}
	else
	{
		average=0;
	}
	if (average&lt;=3)
	{
		return 250;
	}
	if (average&gt;=4&amp;&amp;average&lt;=5)
	{
		return 500;
	}
	if (average&gt;=6)
	{
		return 1000;
	}
	return -1;
}
		


CharSet HowEasy::whatChar(char ch)
{
	if (isAlpha(ch))
	{
		return Alphabetic;
	}
	if (ch==' ')
	{
		return Space;
	}
	if (ch=='.')
	{
		return Period;
	}
	if (ch=='\0')
	{
		return Null;
	}
	return Others;
}
bool HowEasy::isAlpha(char ch)
{
	return ch&gt;='a'&amp;&amp;ch&lt;='z'||ch&gt;='A'&amp;&amp;ch&lt;='Z';
}

State HowEasy::automata(CharSet ch, State state)
{
	switch (state)
	{
	case StartString:
		 total=0;
		 current=0;
		 wordCounter=0;		 
		 switch(ch)
		 {
		 case Alphabetic:		
		 	current++; 	
		 	return InsideWord;
		 case Space:
		 	return BeforeWord;
		 case Null:
		 	return EndString;
		 default:		 	
		 	return NoneWord;
		 }		
		 break;
	case InsideWord:
		switch(ch)
		{
		case Alphabetic:
			current++;
			return InsideWord;
		case Space:
			wordCounter++;
			total+=current;
			current=0;
			return BeforeWord;
		case Period:
			//current++;
			return PeriodWord;
		case Null:
			wordCounter++;
			total+=current;
			current=0;
			return EndString;
		default:
			current=0;
			return NoneWord;
		}
		break;
	case BeforeWord:
		switch (ch)
		{
		case Alphabetic:
			current++;
			return InsideWord;
		case Space:
			return BeforeWord;
		case Null:
			return EndString;
		default:
			return NoneWord;
		}
		break;
	case NoneWord:
		switch(ch)
		{
		case Space:
			return BeforeWord;
		case Null:
			return EndString;
		default:
			return NoneWord;
		}
		break;
	case PeriodWord:
		switch(ch)
		{
		case Space:
			//period is not a letter
			total+=current;
			wordCounter++;
			current=0;
			return BeforeWord;
		case Null:
			total+=current;
			current=0;
			wordCounter++;
			return EndString;
		default:
			current=0;
			return NoneWord;		
		}
	}
	return ErrorState;
}</pre>
	<pre>
</pre>
</div>
<div align="left">
  　</div>
<div align="left">
  <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>