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 7Output
3 1 2Input
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 11Output
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;
}