A.提取子序列 subarr
题目链接-A.提取子序列 subarr
解题思路
- 用
记录能取出的
Jyouhou
串的数量, 表示字符串 的下标 - 从头遍历输入的字符串,每匹配一个字符,就进行
j++
操作,以匹配下一个字符,当j==a.length()
时,说明已经匹配了一个Jyouhou
串,就cnt++
计数,将 置为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;
}