uestc oj 1035 论文搜索(子串查找)
来源:互联网 发布:老七贸易知乎 编辑:程序博客网 时间:2024/06/04 18:41
链接:http://www.acm.uestc.edu.cn/problem.php?pid=1035
代码:
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int main(){int t, n;char keyword[21], title[101];while (scanf("%d",&t) == 1){while (t--){while (scanf("%d",&n) == 1){getchar();gets(keyword);int count = 0;while (n--){gets(title);char *pToken = strtok(title," ");while (pToken != NULL){if (strcmp(pToken,keyword) == 0){count++;break;}pToken = strtok(NULL," ");}}if (count)printf("%d\n\n",count);else printf("Do not find\n\n");}}}//system("pause");return 0;}论文的标题的字符串是由空格隔开,可拆成一个一个的单词,然后和查询的字符串比较即可。
将标题字符串拆分可以用普通处理字符串的方式实现——麻烦
应掌握更简便的方法:使用各种字符串处理函数:(#include <string.h>)
C语言:strtok
C++:istringstream
除此之外,还有如:strcspn ,strstr ,strlen ,strncat,strncmp ,strncpy ,strpbrk 等
strtok()用来将字符串分割成一个个片段。参数s指向欲分割的字符串,参数delim则为分割字符串中包含的所有字符。当strtok()在参数s的字符串中发现参数delim中包含的分割字符时,则会将该字符改为\0 字符。在第一次调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回指向被分割出片段的指针。
char *strtok(char *s, char *delim);
功能:分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。实质上的处理是,strtok在s中查找包含在delim中的字符并用NULL(’\0′)来替换,直到找遍整个字符串。
说明:首次调用时,s指向要分解的字符串,之后再次调用要把s设成NULL。strtok在s中查找包含在delim中的字符并用NULL('\0')来替换,直到找遍整个字符串。
返回值:从s开头开始的一个个被分割的串。当没有被分割的串时则返回NULL。所有delim中包含的字符都会被滤掉,并将被滤掉的地方设为一处分割的节点。
/* strtok example */#include <stdio.h>#include <string.h>int main (){ char s[100]; printf("请输入一行字符串:"); gets(s); printf("下面显示的将是通过空格和分号和惊叹号分割产生的字符串\n"); char *pch = strtok(s," ;!"); while (pch != NULL) { printf ("%s\n",pch); pch = strtok (NULL, " ;!"); } return 0;}参考:http://blog.csdn.net/wuxinliulei/article/details/9060305
- uestc oj 1035 论文搜索(子串查找)
- uestc oj 1035 论文搜索
- UESTC OJ 1086 邱老师降临小行星 记忆化搜索
- 论文数据查找和论文参考准确搜索定位
- uestc oj 1793 Kalindrome
- uestc oj 1227 Hotel
- uestc oj 1500 AreYouBusy
- uestc oj 1484 STAMPS
- uestc oj 1272 Sticks
- uestc oj 1230 Picture
- uestc oj 1223 Islands
- uestc oj 1001 DotNotation
- 吐槽 uestc oj
- uestc oj 1876 聚会
- UESTC OJ 1830
- UESTC OJ Windy 数
- FAFU OJ 子串
- oj 回文子串
- OCP-1Z0-053-V12.02-661题
- 深入理解typedef
- windows下命令行编译zlib
- JQuery学习系列总结—菜单制作
- 如何快速的学习并使用一门语言工作-3(原创)
- uestc oj 1035 论文搜索(子串查找)
- 正则表达式学习笔记
- mysql书库表检查
- phpize和php-config安装模块
- 深入理解指针函数
- 线程
- 智能语义搜索 - 徐海蛟
- c 中的 # #@ ##
- 启动服务器的时候遇到的org.hibernate.PropertyNotFoundException: field [set] not found on xx.xxx.xx问题