提取字符串序列

一道求取字符串子序列的题目



#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int solution(string s, string target) {
    int cnt = 0;

    int index = 0;
    int last = 0;

    while (1) {
        int j = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s[i] == target[j]) {
                j++;
                if (j == target.length() ) {
                    j = 0;
                    s = s.substr(i);
                    cnt++;

                    break;
                }
            }

        }
        if (last == cnt)
            return cnt;
        else last = cnt;

    }


}

int main() {
    int T;
    string target = "Jyouhou";
    string s;
    cin >> T;
    while (T--) {
        int n;
        cin  >> n >> s;
        int x = solution(s, target);
        cout << x << endl;

    }

    return 0;
}
posted @ 2020-04-03 17:26  DengSchoo  阅读(161)  评论(0编辑  收藏  举报