2020年9月12日普及组 T1 字符串【模拟】

在这里插入图片描述

这道题还是比较水的

枚举 i i i,暴力找到 i i i 之后第一个出现的 b e a r bear bear 的位置,
然后直接将后面所有字母个数累加即可。

谁能告诉我 O ( n ) O(n) O(n) 的方法怎么做!!!私我!!!

代码:

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
long long vis[100010],ans;
string s;
int main()
{
    
    
	cin>>s;
	for(int i=0; i<=s.size()-1; i++)
	 for(int j=i; j<=s.size()-1; j++)
	  if(s[j]=='b'&&s[j+1]=='e'&&s[j+2]=='a'&&s[j+3]=='r')
	   {
    
    
	  	  ans=ans+((s.size()-1)-(j+3))+1;
	  	  break;
	   }
	cout<<ans;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Jackma_mayichao/article/details/108549904
今日推荐