状态机学习(一)统计字符
题目来自<系统程序员成长计划>
作者:李先静.
逐个字符扫描 根据是否为字母决定当前状态 由单词内状态切换到单词外 计数字符加1
#include <iostream> #include <string> #include <vector> using namespace std; string testString = " this's a test,'Hello World!!!' Welcome to the real world, it sucks, but you are gona love it!!!"; enum STATE { STAT_INIT, STAT_IN_WORD, STAT_OUT_WORD }; size_t CountWords(const string& s) { int count = 0; STATE state = STAT_INIT; for (size_t i = 0; i < s.size(); i++) { switch (state) { case STAT_INIT: if (isalpha(s[i])) { state = STAT_IN_WORD; }else { state = STAT_OUT_WORD; } break; case STAT_IN_WORD: if (isalpha(s[i])) { state = STAT_IN_WORD; } else { count++; state = STAT_OUT_WORD; } break; case STAT_OUT_WORD: if (isalpha(s[i])) { state = STAT_IN_WORD; } else { state = STAT_OUT_WORD; } break; default: break; }//switch (state) } if (STAT_IN_WORD == state) { count++; } return count; } int main() { cout << CountWords(testString) << endl; return 0; }
改进版 计数以外还讲单词放入容器
#include <iostream> #include <string> #include <vector> using namespace std; string testString = " this's a test,'Hello World!!!' Welcome to the real world, it sucks, but you are gona love it!!!"; enum STATE { STAT_INIT, STAT_IN_WORD, STAT_OUT_WORD }; size_t CountWords(const string& s) { int count = 0; STATE state = STAT_INIT; for (size_t i = 0; i < s.size(); i++) { switch (state) { case STAT_INIT: if (isalpha(s[i])) { state = STAT_IN_WORD; }else { state = STAT_OUT_WORD; } break; case STAT_IN_WORD: if (isalpha(s[i])) { state = STAT_IN_WORD; } else { count++; state = STAT_OUT_WORD; } break; case STAT_OUT_WORD: if (isalpha(s[i])) { state = STAT_IN_WORD; } else { state = STAT_OUT_WORD; } break; default: break; }//switch (state) } if (STAT_IN_WORD == state) { count++; } return count; } size_t word_segmentation(const string& s, vector<string>& strVec) { STATE state = STAT_INIT; size_t count = 0; size_t subStrStart = string::npos; size_t subStrEnd = string::npos; for (size_t i = 0; i < s.size(); i++) { switch (state) { case STAT_INIT: if (isalpha(s[i])) { subStrStart = i; state = STAT_IN_WORD; } break; case STAT_IN_WORD: if (!isalpha(s[i])){ count++; subStrEnd = i; strVec.push_back(s.substr(subStrStart, subStrEnd- subStrStart)); state = STAT_OUT_WORD; } break; case STAT_OUT_WORD: if (isalpha(s[i])) { subStrStart = i; state = STAT_IN_WORD; } break; default: break; }//switch (state) } if (state == STAT_IN_WORD) { count++; subStrEnd = s.size(); strVec.push_back(s.substr(subStrStart, subStrEnd- subStrStart)); } return count; } int main() { cout << CountWords(testString) << endl; vector<string> strVec; cout << word_segmentation(testString, strVec) << endl; for (vector<string>::iterator it = strVec.begin(); it != strVec.end(); it++) { cout << *it << " "; } cout << endl; return 0; }
运行结果
19
19
this s a test Hello World Welcome to the real world it sucks but y
ou are gona love it
请按任意键继续. . .
作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
![阿里打赏](https://files.cnblogs.com/files/itdef/ali2.bmp)
![微信打赏](https://files.cnblogs.com/files/itdef/weixin2.bmp)