Distinct Characters Queries (不同字符查询)

http://codeforces.com/problemset/problem/1234/D

You are given a string ss consisting of lowercase Latin letters and qq queries for this string.

Recall that the substring s[l;r]s[l;r] of the string ss is the string slsl+1…srslsl+1…sr . For example, the substrings of "codeforces" are "code", "force", "f", "for", but not "coder" and "top".

There are two types of queries:

  • 1 pos c (1≤pos≤|s| , c is lowercase Latin letter): replace sposspos with c (set spos:=c );
  • 2 l r (1≤l≤r≤|s| ): calculate the number of distinct characters in the substring s[l;r] .

Input

The first line of the input contains one string ss consisting of no more than 105 lowercase Latin letters.

The second line of the input contains one integer q (1≤q≤105 ) — the number of queries.

The next q lines contain queries, one per line. Each query is given in the format described in the problem statement. It is guaranteed that there is at least one query of the second type.

Output

For each query of the second type print the answer for it — the number of distinct characters in the required substring in this query.

Examples

Input

abacaba
5
2 1 4
1 4 b
1 5 b
2 4 6
2 1 7

Output

3
1
2

Input

dfcbbcfeeedbaea
15
1 6 e
1 4 b
2 6 14
1 7 b
1 12 c
2 6 8
2 1 6
1 7 c
1 2 f
1 10 a
2 7 9
1 10 a
1 14 b
1 1 f
2 1 11

Output

5
2
5
2
6

 题意:

先给一个字符串;n次查询,每次有三个数a,b,c,当a=1时,把第b个位置的值变成c;当a=2时,查询b~c中有多少个不相同的字符并输出。

思路:

set容器储存,特点就是不会重复插入(在这个题是关键),也有自动排序~等等,如果学会容器,做题真的是很方便啊~~我还能说些什么~~好好学习吧~

 

 Code:

#include<stdio.h>
#include<string.h>
#include<set>
typedef long long ll;
const int N=1e5+10;
using namespace std;
char s[N],str;///字符串   要改变的字符
int a,b,c,d;///a次查询   b(1/2)  
set<int>st[30];///st数组储存  26个小写字符  原字符串中的第几个位置
set<int>::iterator it;///迭代器遍历
int main()
{
    memset(s,0,sizeof(s));///清空
    for(int i=1; i<=26; i++)
        st[i].clear();///清空set
    scanf("%s",s+1);///1~26
    int ans;///结果
    int l=strlen(s+1);
    for(int i=1; i<=l; i++)
        st[s[i]-'a'+1].insert(i);/// 数组中储存的是该字符在原串中的位置
    scanf("%d",&a);
    for(int i=1; i<=a; i++)///a次查询
    {
        scanf("%d",&b);
        if(b==1)///修改
        {
            scanf("%d %c",&c,&str);
            st[s[c]-'a'+1].erase(c);///删除此位置的值
            s[c]=str;///重新赋值
            st[s[c]-'a'+1].insert(c);///再次在此位置插入要改成的值
        }
        else///查询
        {
            scanf("%d %d",&c,&d);
            ans=0;
            for(int i=1; i<=26; i++)
            {
                it=st[i].lower_bound(c);///it指针从大于等于c这个位置的值开始查询  
                if(it==st[i].end())
                    continue;///说明在st[i]的数组中没有大于等于c位置的字符  跳出循环
                if(*it<=d)///说明st[i]数组中有c~d位置的字符   记录下来
                    ans++;
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嵩韵儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值