码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #342 (Div. 2) B. War of the Corporations(贪心)

时间:2016-11-05 02:19:53      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:turn   ring   dir   devices   include   was   tin   sample   std   

传送门

Description

A long time ago, in a galaxy far far away two giant IT-corporations Pineapple and Gogol continue their fierce competition. Crucial moment is just around the corner: Gogol is ready to release it‘s new tablet Lastus 3000.

This new device is equipped with specially designed artificial intelligence (AI). Employees of Pineapple did their best to postpone the release of Lastus 3000 as long as possible. Finally, they found out, that the name of the new artificial intelligence is similar to the name of the phone, that Pineapple released 200 years ago. As all rights on its name belong to Pineapple, they stand on changing the name of Gogol‘s artificial intelligence.

Pineapple insists, that the name of their phone occurs in the name of AI as a substring. Because the name of technology was already printed on all devices, the Gogol‘s director decided to replace some characters in AI name with "#". As this operation is pretty expensive, you should find the minimum number of characters to replace with "#", such that the name of AI doesn‘t contain the name of the phone as a substring.

Substring is a continuous subsequence of a string.

Input

The first line of the input contains the name of AI designed by Gogol, its length doesn‘t exceed 100 000 characters. Second line contains the name of the phone released by Pineapple 200 years ago, its length doesn‘t exceed 30. Both string are non-empty and consist of only small English letters.

Output

Print the minimum number of characters that must be replaced with "#" in order to obtain that the name of the phone doesn‘t occur in the name of AI as a substring.

Sample Input

intellect
tell

google
apple

sirisiri
sir

Sample Output

1

0

2

Note

In the first sample AI‘s name may be replaced with "int#llect".

In the second sample Gogol can just keep things as they are.

In the third sample one of the new possible names of AI may be "s#ris#ri".

思路

题意:

给出字符串a和字符串b,问至少修改多少个字符,使得字符串a没有一个子串为b

题解:

从头到尾扫一遍,当出现字符串a的子串与b相同,尽可能修改最后的字符,使得最后结果最小。例如:a: sisisis  b: sis  这样的策略下,只需修改两个字符即可。

 

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100005;
char a[maxn];
char b[35],tmp[35];

int main()
{
	int cnt = 0;
	scanf("%s %s",a,b);
	int lena = strlen(a);
	int lenb = strlen(b);
	for (int i = 0;i <= lena - lenb;)
	{
		strncpy(tmp,a+i,lenb);
		if (strcmp(tmp,b) == 0)
		{
			cnt++;
			i += lenb;
		}
		else
		{
			i++;
		}
	}
	printf("%d\n",cnt);
	return 0;
}

  

 

Codeforces Round #342 (Div. 2) B. War of the Corporations(贪心)

标签:turn   ring   dir   devices   include   was   tin   sample   std   

原文地址:http://www.cnblogs.com/zzy19961112/p/6032230.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!