子串查询

子串查询

Time Limit: 3500/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 315    Accepted Submission(s): 163


 

Problem Description

度度熊的字符串课堂开始了!要以像度度熊一样的天才为目标,努力奋斗哦!

为了检验你是否具备不听课的资质,度度熊准备了一个只包含大写英文字母的字符串 A[1,n]=a1a2⋯an,接下来他会向你提出 q 个问题 (l,r),你需要回答字符串 A[l,r]=alal+1⋯ar 内有多少个非空子串是 A[l,r] 的所有非空子串中字典序最小的。这里的非空子串是字符串中由至少一个位置连续的字符组成的子序列,两个子串是不同的当且仅当这两个子串内容不完全相同或者出现在不同的位置。

记 |S| 为字符串 S 的长度,对于两个字符串 S 和 T ,定义 S 的字典序比 T 小,当且仅当存在非负整数 k(≤min(|S|,|T|)) 使得 S 的前 k 个字符与 T 的前 k 个字符对应相同,并且要么满足 |S|=k 且 |T|>k,要么满足 k<min(|S|,|T|) 且 S 的第 k+1 个字符比 T 的第 k+1 个字符小。例如 "AA" 的字典序比 "AAA" 小,"AB" 的字典序比 "BA" 小。

 

Input

第一行包含一个整数 T,表示有 T 组测试数据。

接下来依次描述 T 组测试数据。对于每组测试数据:

第一行包含两个整数 n 和 q,表示字符串的长度以及询问的次数。

第二行包含一个长为 n 的只包含大写英文字母的字符串 A[1,n]。

接下来 q 行,每行包含两个整数 li,ri,表示第 i 次询问的参数。

保证 1≤T≤10,1≤n,q≤105,1≤li≤ri≤n。

 

Output

对于每组测试数据,先输出一行信息 "Case #x:"(不含引号),其中 x 表示这是第 x 组测试数据,接下来 q 行,每行包含一个整数,表示字符串 A[l,r] 中字典序最小的子串个数,行末不要有多余空格。

 

Sample Input

 

1

2 3

AB

1 1

1 2

2 2

 

Sample Output

 

Case #1:

1

1

1

AC代码,我是用线段树的。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1<<19;
const int MAXN = 1e5 + 10;
char s[MAXN];
struct node{
    int left;
    int right;
    char c;
    int value;
}tree[maxn];
//int father[maxn<<1];
void BuildTree(int l,int r,int i)// 0 1 0
{
    tree[i].left = l;   // 0  0  1
    tree[i].right = r;
    if(l == r){ //根节点
        tree[i].c = s[l];
        tree[i].value = 1;
        //father[left] = i;
        return ;
    }
    else{
        char chmain = s[l]; // A
        int ans = 1;  //0
        for(int j=l+1;j<=r;j++){ //1
             if(chmain > s[j]){ //A B  ABCADACBDC
                 ans = 1;
                 chmain = s[j];
             }
             else if(chmain == s[j]){
                ans++;
             }
        }
        //cout <<ans << endl;
        tree[i].c = chmain;   //A
        tree[i].value = ans;  //1

    }//cout << i << ' ' << tree[i].value << endl;
    BuildTree(l,(int)floor(l+r)/2.0,(i<<1)+1); //向左子树遍历    0 0 1
    BuildTree((int)(floor(r+l)/2.0+1),r,(i<<1)+2);//向右子树遍历 1 1 2
}
int answer;
char  ch;
void Query(int left,int right,int i) //查询 1 1 0
{
    if(tree[i].left == left && tree[i].right == right){//区间相等   0
        if(ch>tree[i].c){          //
            answer = tree[i].value;
            ch=tree[i].c;
        }
        else if(ch==tree[i].c){
            answer += tree[i].value;
        }
       // cout << ch << endl;
        return;
    }
    i = i<<1 ; //
    i++;//1
    if(left <= tree[i].right){
        if(right <= tree[i].right) Query(left,right,i);
        else Query(left,tree[i].right,i);
    }
    i++;
    if(right >= tree[i].left){
        if(left >= tree[i].left) Query(left,right,i);
        else Query(tree[i].left,right,i);
    }
}
int main()
{
    int T,n,m,a,b;
    int kiss = 1;
    scanf("%d",&T);
    while(T--){
        scanf("%d %d",&n,&m);
        getchar();
        scanf("%s",s);
        printf("Case #%d:\n",kiss);
        kiss++;
        BuildTree(0,n-1,0);
        while(m--){
            scanf("%d %d",&a,&b);
            answer = 0;
            ch = 'Z';
            Query(a-1,b-1,0);
            printf("%d\n",answer);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/blackneed/article/details/81477315