统计单词数
2012-04-03 16:20
183 查看
#include <iostream> #include <sstream> #include <stdio.h> #include <string> #include <cstring> #include <cctype> using namespace std; /* 耗时什么的,完全不在乎了,已经被搞吐了 */ void stolower(string& str) { for(int i = 0; i < str.size(); i++) if(isalpha(str[i])) str[i] = tolower(str[i]); } int pos[1000000 + 10]; int main() { string key, word; string article; char c_key[30]; while(cin.peek() == ' ') cin.ignore(); cin.getline(c_key, 30); if (cin.peek() == '\n') cin.ignore(); for(int i = 0; i < 15; i++) { if(!isalpha(c_key[i])) { c_key[i] = 0; break; } } key = c_key; stolower(key); getline(cin, article); stolower(article); int m = 0; for(int i = 0; i < article.length(); i++) { if(article[i] != ' ' && (article[i - 1] == ' ' || i == 0)) pos[m++] = i; } stringstream s_stream(article); int first = 0, firstpos = 0, count = 0, charcount = 0; int npass = 0; while(s_stream >> word) { if(word == key) { if(!first) { firstpos = pos[npass]; first = 1; } count++; } npass++; } if(!first) printf("%s\n", "-1"); else printf("%d %d\n", count, firstpos); return 0; }
相关文章推荐
- 统计文本行单词数
- 输入有话统计这句话有多少单词
- 统计一组字符串中各个单词出现的次数
- 洛谷P1308 统计单词数
- NOIP 2001 统计单词个数 解题报告
- 统计一段语句的单词个数
- 开发可统计单词个数的Android驱动程序(3)
- 【日常学习】【字符串处理】noip2011普及组第2题 统计单词数题解
- 练习2-4:编一个程序统计文件中特定单词出现的次数(要求使用string类的运算符==来查找单词) .
- 一道笔试题:统计文件中的单词出现次数
- 有一行字符,统计其中的单词个数(单词之间以空格分隔),并将每一个单词的第一个字母改为大写
- 题目1182:统计单词
- 【Java字符串高效率】统计单词统计个数
- 算法提高 ADV-17 统计单词数
- 蓝桥杯-问题 1505:单词个数统计(c++ string,vector解决)
- Java案例:统计单词个数
- 输入一行字符,统计其中有多少个单词,单词之间用空格分隔开
- C语言实现统计字符串单词数
- 实现一个字符串中单词个数的统计,并按照单词字典序输出单词以及单词的出现个数。使用strsep
- 单词个数统计