您的位置:首页 > 产品设计 > UI/UE

UVa 123 Searching Quickly

2013-04-18 20:30 344 查看
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cctype>
#include <cstdlib>

using namespace std;
struct key{
char keywords[100]; //关键字
int line; //所在行
int start; //行偏移位置
};

key keys[3000];
int num_key = 0;

char ignore[60][15];
int num_ig = 0;
char titles[250][300];
int num_tit = 0;

//qsort使用的比较函数
int cmp_key(const void *a, const void *b) {
key *_a = (key *)a;
key *_b = (key *)b;
int ret = strcmp(_a->keywords, _b->keywords);
if(ret == 0) {
if(_a->line == _b->line) {
return _a->start - _b->start;
} else {
return _a->line - _b->line;
}
} else {
return ret;
}
}
// tilte转换为小写
void titles_lower()
{
for(int i=0; i<num_tit; i++) {
char *p = titles[i];
while(*p) {
*p = tolower(*p);
p++;
}
}
}
// 判断关键字是否在ignore中
bool in_ignore(const char *words) {
for(int i=0; i<num_ig; i++) {
if(strcmp(words, ignore[i]) == 0) return true;
}
return false;
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif

while(gets(ignore[num_ig])) {
if(ignore[num_ig][0] == ':') break;
else num_ig++;
}
//for(int i=0; i<num_ig; i++) puts(ignore[i]);
while(gets(titles[num_tit++]));
titles_lower();
//for(int i=0; i<num_tit; i++) puts(titles[i]);
key k;
char *p;
for(int i=0; i<num_tit; i++) { //获得keyword
p = titles[i];
while(sscanf(p, "%s", k.keywords) == 1) {
if(!in_ignore(k.keywords)) {
k.line = i;
k.start = p-titles[i];
keys[num_key++] = k;
}
p += strlen(k.keywords);
while(*p == ' ') p++; //排除空格
}
}
qsort(keys, num_key, sizeof(key), cmp_key); // keyword排序
char buff[300];
for(int i=0; i<num_key; i++) {
strcpy(buff, titles[keys[i].line]);
p = buff + keys[i].start;
char *q = p + strlen(keys[i].keywords);
while(p<q) {
*p = toupper(*p);
p++;
}
puts(buff);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: