1. 程式人生 > >唐納德與子串 (Easy)(計運算元串個數)

唐納德與子串 (Easy)(計運算元串個數)

Time limit per test: 1.0 seconds
Memory limit: 256 megabytes
子串的定義是在一個字串中連續出現的一段字元。這裡,我們使用 s[l…r] 來表示 s 字串從 l 到 r(閉區間)的子串。在本題中,字串下標從 0 開始。顯然,對於長度為 n 的字串共有 n(n+1)2 個子串。
對於一個給定的字串 s,唐納德給出 q 次詢問,第 i 次詢問包括三個引數 li,ri,zi,問在 s[li…ri] 的所有子串中共有多少個恰好為 zi。

Input
輸入具有如下形式:

sql1 r1 z1l2 r2 z2⋮lq rq zq

第一行一個字串 s。

第二行一個整數 q。

接下來每行:首先兩個整數 li,ri (0≤li≤ri<|s|),然後是一個非空字串 zi。整數和整數,整數和字串間以單空格隔開。

字串中只會出現 26 個小寫英文字母。

資料規模約定:

對於 Easy 檔:1≤|s|≤100,q≤∑|zi|≤100。
對於 Hard 檔:1≤|s|≤105,q≤∑|zi|≤105。
Output
對於每次詢問,輸出一個整數,表示答案。

Examples
input
thisisagarbagecompetitionhahaha
5
0 30 a
1 5 is
25 30 hah
6 12 ag
7 12 ag
output
6
2
2
2
1

#include <iostream>
#include <cstdio>
#include <string>

using namespace std;

int main()
{
    string str;
    while(cin>>str)
    {
        int q;
        cin>>q;
        while(q--)
        {
            int a,b;
            string ch;
            cin>>a>>b>>ch;
            string
s=str.substr(a,b-a+1); int ans=0; while(s.find(ch)<100) { int pos=s.find(ch); s=s.substr(pos+1); ans++; } cout<<ans<<endl; } } return 0; }