题目
1178: 单词数
时间限制: 1 Sec 内存限制: 128 MB
提交: 7017 解决: 2506
统计一篇文章里不同单词的总数。
输入
有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由大小写字母和空格组成,没有标点符号,遇到#时表示输入结束。每篇文章的单词数小于1000,每个单词最多由30个字母组成。
输出
每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。
样例输入 Copy
you are my friend
样例输出 Copy
4
代码
#include <iostream>
#include <string>
#include <cctype>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
string texts, sub_word;
vector<string> svec;
int cnt = 0;
int exitFlag = 0;
int flag = 0; // 标记是否找到单词
getline(cin, texts);
while (!(texts == "#")) {
exitFlag = 0;
for (auto it = texts.begin(); it != texts.end(); it++) {
if(isalpha(*it)) {
sub_word += *it;
if ((it + 1) == texts.end() || !isalpha(*(it + 1))) {
if ((it + 1) == texts.end())
{
for (auto it = svec.begin(); it != svec.end(); it++)
{
if (*it == sub_word)
{
flag = 1;
sub_word = "";
}
}
if (flag == 0)
{
svec.push_back(sub_word);
sub_word = "";
}
flag = 0;
}
else if (*(it + 1) != '#')
{
for (auto it = svec.begin(); it != svec.end(); it++) {
if (*it == sub_word) {
flag = 1;
sub_word = "";
}
}
if (flag == 0) {
svec.push_back(sub_word);
sub_word = "";
}
flag = 0;
}
else {
for (auto it = svec.begin(); it != svec.end(); it++) {
if (*it == sub_word) {
flag = 1;
sub_word = "";
}
}
if (flag == 0) {
svec.push_back(sub_word);
sub_word = "";
}
cout << svec.size() << endl;
exitFlag = 1;
break;
}
}
}
else if (*it == '#') {
exitFlag = 1;
if (svec.size() > 0)
cout << svec.size() << endl;
break;
}
}
if (exitFlag == 0) {
cout << svec.size() << endl;
svec.clear();
getline(cin, texts);
}
else break;
}
return 0;
}
总结
没有勇气实现模块化,代码快超过100行了!!!
刷题妖精!!!加油,加油!