uva 409 poj 1598
来源:互联网 发布:苹果手机的mac地址在哪 编辑:程序博客网 时间:2024/04/28 00:04
uva 409和poj 1598是同一道题。这道题也算是基础题了,主要有两点需要注意
1. A keyword "occurs" in an excuse if and only if it exists in the string in contiguous form and is delimited by the beginning or end of the line or any non-alphabetic character or a space. 所以说并不是简单地在一个字符串中寻找特定的子串,这个子串的前后需要是句子的开始或结束或是符合要求的分隔符。
2. 题目中说明了可能出现符号{.,!?}, 但是我第一次用这些符号做匹配时wa了,之后就把字符串中所有不是字母的字符全换为空格,这样更好处理
#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string>#include <string.h>#include <memory>using namespace std;int knum, enumber;const int maxn = 20 + 10;string keywords[maxn], execuses[maxn], ori[maxn];int cnt[maxn];void read(){for (int i = 0; i < knum; i++)cin >> keywords[i];getchar();for (int i = 0; i < enumber; i++){getline(cin, execuses[i]);ori[i] = execuses[i];}for (int i = 0; i < enumber; i++){for (int j = 0; j < execuses[i].length(); j++){if (isalpha(execuses[i][j]))execuses[i][j] = tolower(execuses[i][j]);elseexecuses[i][j] = ' ';}}}bool validSep(char ch){if (ch == ' ' || ',' == ch || '.' == ch || '?' == ch || '!' == ch)return true;return false;}void search(){for (int i = 0; i < enumber; i++){for (int j = 0; j < knum; j++){int start = 0, pos = 0;while (true){pos = execuses[i].find(keywords[j], start);if (pos == string::npos)break;int endPos = pos + keywords[j].length();if (endPos >= execuses[i].length() || validSep(execuses[i][endPos])){if (0 == start || validSep(execuses[i][start - 1]))cnt[i]++;};start = pos + keywords[j].length();}}}//for enumber}int main(){freopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout);int kase = 0;while (cin >> knum >> enumber){read();memset(cnt, 0, sizeof(cnt));search();int cntMax = -1;for (int i = 0; i < enumber; i++){if (cntMax < cnt[i])cntMax = cnt[i];}cout << "Excuse Set #" << ++kase << endl;for (int i = 0; i < enumber; i++){if (cntMax == cnt[i])cout << ori[i] << endl;}cout << endl;}//whilereturn 0;}
0 0
- uva 409 poj 1598
- UVA (POJ 1598)Excuses, Excuses!
- HDU 1606(POJ 1598 UVA 409) Excuses, Excuses!(字符串匹配处理)
- uva 1394&poj 3517
- POJ 2662 UVA 10917
- poj 1852 && uva 10881
- uva 657 poj 1481
- poj 2251 uva 532
- poj 2325 && uva 10527
- poj 2249 && uva 530
- POJ 3368 & UVA 11235
- POJ 1003 & UVA 2294
- POJ 1338 & UVA 136
- poj 3523 uva 3523
- poj 1410 || UVA 191 Intersection
- poj 2560 uva 10034 - Freckles
- uva 10032拔河 poj 2576
- uva 10972 类似poj 3352
- Java编程思想
- 虚拟机下配置caffe
- Android优化(一)_Java代码优化
- pg源码阅读五
- Spring mvc实现文件上传
- uva 409 poj 1598
- 7.5个数求最值
- 静态块 静态方法 构造方法 普通方法 执行顺序
- 二分查找法
- iOS9关键字的简单实用
- 8.蛇形填数
- 面对对象总结
- ({}+{}).length
- 设计模式---行为类型---策略