UVa 494 Kindergarten Counting Game
题目描述题目要求统计每行输入中单词的数量。单词定义为连续的字母序列大小写均可由非字母字符分隔。输入格式输入包含多行每行包含多个单词至少一个。输入以文件结束符EOF\texttt{EOF}EOF终止。输出格式对于每行输入输出一行一个整数表示该行中单词的数量。样例输入Meep Meep! I tot I taw a putty tat. I did! I did! I did taw a putty tat. Shsssssssssh ... I am hunting wabbits. Heh Heh Heh Heh ...输出2 7 10 9题目分析本题的核心是统计字符串中由字母组成的连续子串的数量。方法一逐字符扫描遍历每行字符串遇到字母时开始计数并跳过连续的字母直到遇到非字母字符然后继续扫描。注意边界处理。方法二正则表达式使用正则表达式[a-zA-Z]匹配所有单词然后计数匹配项。复杂度分析每个字符处理一次时间复杂度O(L)O(L)O(L)。代码实现// Kindergarten Counting Game// UVa ID: 494// Verdict: Accepted// Submission Date: 2017-12-27// UVa Run Time: 0.000s//// 版权所有C2017邱秋。metaphysis # yeah dot net#includebits/stdc.husingnamespacestd;intmain(intargc,char*argv[]){cin.tie(0),cout.tie(0),ios::sync_with_stdio(false);string patternR([a-zA-Z]);regexe(pattern,regex_constants::icase);string line;while(getline(cin,line)){regex_iteratorstring::iteratorit(line.begin(),line.end(),e);regex_iteratorstring::iteratorend;intcount0;while(it!end){count;it;}coutcount\n;}return0;}