本文主要是介绍123 - Searching Quickly,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:123 - Searching Quickly
题目大意:要求将输入的title里的单词,除了他明确声明不要的可以忽视的,其余的作为关键字,找每个句子中这个关键字的位置,整个句子输出,将关键字标为大写,其余的字符小写。还有输出要关键字的字典序输出,相同关键字按碰见位置先后。
解题思路:将titles 转换成小写的,分解成关键字单词。关键字qsort()排列成字典序,删重,便于后面输出时的先后控制。然后用strstr()找关键字出现的位置,输出,注意不是找到一个位置并就好了,要找到末尾,或是找不到为止。还要注意你找的是单词,前面要不没有字符,要不就是空格。单词长度后的第一个字符要不是空格,就是‘\0’。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<string>
#include<iostream>
using namespace std;const int N = 55;
const int M = 15;
const int R = 205;
const int C = 10005;char title[R][C];
char ignword[N][M];
char keyword[C][C];
int n = 0, n1 = 0, n2 = 0;void takeword () {int i, j, k = 0;bool bo = 0;for( i = 0; i < n1; i++ ) {for(j = 0; j <= strlen(title[i]); j++) {if( title[i][j] >= 'a' && title[i][j] <= 'z') {keyword[n2][k++] = title[i][j];bo = 1;}else if(bo){keyword[n2][k] = '\0';n2++;k = 0;bo = 0;}}}for(i = 0; i < n2; i++) {for(k = 0; k < n; k++) {if(strcmp(keyword[i], ignword[k]) == 0) {keyword[i][0] = '\0';break;}}}
}void translate() {for(int i = 0; i < n1; i++) {for(int j = 0; j < strlen(title[i]); j++) {if(title[i][j] >= 'A' && title[i][j] <= 'Z')title[i][j] += 32;}}
}int cmp(const void * _a, const void * _b) {char *a = (char*) _a;char *b = (char*) _b;return strcmp(a, b);
}void deleted() {for(int i = 0; i < n2; i++) {if(i + 1 < n2) {if(strcmp(keyword[i],keyword[i + 1]) == 0)keyword[i][0] = '\0';}}
}int main() {int i , j, k;char * t = NULL;while(scanf("%s",ignword[n])) {if(strcmp(ignword[n],"::") == 0)break;n++;}while(cin.getline(title[n1], R)) {n1++;}translate();takeword();qsort(keyword, n2, sizeof(keyword[0]), cmp);deleted();for(i = 0; i < n2; i++) if(strcmp(keyword[i],"") != 0) {for(j = 0; j < n1; j++) {t = strstr(title[j], keyword[i]);while(t != NULL) {if((t == title[j] || *(t - 1) == ' ') && (*(t + strlen(keyword[i])) == ' ' || *(t +strlen(keyword[i])) == '\0')) {char * t1 = title[j];for(; *t1 != '\0'; t1++) {if(t1 != t)printf("%c", *t1);else {for(k = 0; k < strlen(keyword[i]); k++)printf("%c", *(t1 + k) - 32);t1 += strlen(keyword[i]) - 1;} } printf("\n");}t = strstr(t + strlen(keyword[i]) - 1, keyword[i]);}}}return 0;
}
这篇关于123 - Searching Quickly的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!