链接: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