zoukankan      html  css  js  c++  java
  • string中 find 查询

    B. Segment Occurrences
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given two strings ss and tt, both consisting only of lowercase Latin letters.

    The substring s[l..r]s[l..r] is the string which is obtained by taking characters sl,sl+1,,srsl,sl+1,…,sr without changing the order.

    Each of the occurrences of string aa in a string bb is a position ii (1i|b||a|+11≤i≤|b|−|a|+1) such that b[i..i+|a|1]=ab[i..i+|a|−1]=a (|a||a| is the length of string aa).

    You are asked qq queries: for the ii-th query you are required to calculate the number of occurrences of string tt in a substring s[li..ri]s[li..ri].

    Input

    The first line contains three integer numbers nn, mm and qq (1n,m1031≤n,m≤103, 1q1051≤q≤105) — the length of string ss, the length of string ttand the number of queries, respectively.

    The second line is a string ss (|s|=n|s|=n), consisting only of lowercase Latin letters.

    The third line is a string tt (|t|=m|t|=m), consisting only of lowercase Latin letters.

    Each of the next qq lines contains two integer numbers lili and riri (1lirin1≤li≤ri≤n) — the arguments for the ii-th query.

    Output

    Print qq lines — the ii-th line should contain the answer to the ii-th query, that is the number of occurrences of string tt in a substring s[li..ri]s[li..ri].

     

    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <iostream>
    using namespace std;
    typedef long long ll;
    
    int main()
    {
        int a,b,c,l,r,i;
        string d,e;
        scanf("%d%d%d",&a,&b,&c);
        cin>>d,cin>>e;
        while(1)
        {
            int p=d.find(e);
            if(p!=-1)
                d[p]='1';
            else
                break;
        }
        while(c--)
        {
            int ans=0;
            cin>>l>>r;
            l--,r--;
            for(i=l;i<=r+1-b;i++)
            {
                if(d[i]=='1')
                    ans++;
            }
            printf("%d
    ",ans);
    
    
    
        }
    
        return 0;
    }
  • 相关阅读:
    Ubuntu 安装.net core 设置源仓储地址
    ASP.NET Core开发-MVC 使用dotnet 命令创建Controller和View
    ubuntu安装postman
    redis相关操作
    C# StringValues 类型
    MySQL时间戳与日期格式的相互转换
    C# 生成时间戳以及时间戳转换为时间
    关于vscode 使用nuget插件相关说明
    ubuntu一些命令
    使用HBuilderX新建Uniapp项目并在不同平台上运行调试
  • 原文地址:https://www.cnblogs.com/bhd123/p/9636693.html
Copyright © 2011-2022 走看看