uestc oj 1035 论文搜索(子串查找)

链接: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中包含的字符都会被滤掉,并将被滤掉的地方设为一处分割的节点。 

C语言中strtok的示例用法:

/* 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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值