赛氪2020年首届算法竞赛网络挑战赛·热身赛 A.提取子序列 subarr

A.提取子序列 subarr

题目链接-A.提取子序列 subarr
在这里插入图片描述
在这里插入图片描述
解题思路

  • c n t cnt 记录能取出的Jyouhou串的数量, j j 表示字符串 a a 的下标
  • 从头遍历输入的字符串,每匹配一个字符,就进行j++操作,以匹配下一个字符,当j==a.length()时,说明已经匹配了一个Jyouhou串,就cnt++计数,将 j j 置为0,以再次匹配
  • 具体操作见代码

附上代码

#include<bits/stdc++.h>
#define int long long
#define lowbit(x) (x &(-x))
#define endl '\n'
using namespace std;
const int INF=0x3f3f3f3f;
const int dir[4][2]={-1,0,1,0,0,-1,0,1};
const double PI=acos(-1.0);
const double e=exp(1.0);
const double eps=1e-10;
const int M=1e9+7;
const int N=1e5+5;
typedef long long ll;
typedef pair<int,int> PII;
typedef unsigned long long ull;
string s,a="Jyouhou";
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	
	int t;
	cin>>t;
	while(t--){
		int n,cnt=0;
		cin>>n>>s;
		for(int i=0,j=0;i<n;i++){
			if(s[i]==a[j])
				j++;
			if(j==a.length()){
				cnt++;
				j=0;
			}
		}
		cout<<cnt<<endl;
	}
	return 0;
}
发布了175 篇原创文章 · 获赞 15 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Fiveneves/article/details/105334948