题目链接
题意:
给定义长度为n(小于等于十的五次方)的字符串,对字符串有有两种操作
操作1:输入1 pos c 将位置pos的字母换成c
操作2:输入2 l r 输出l~r中有多少种不同的字母
样例:
输入:
abacaba
5
2 1 4
1 4 b
1 5 b
2 4 6
2 1 7
输出:
3
1
2
思路:
定义26个set型vector数组,记录每个字母每次出现的位置。
操作1时 更新数组
操作2时 每次只需要二分查找26个vector数组中有没有从l到r之间的数即可。
(注意二分查找的方法。可以用lower_bound(),但是注意有一种会超时)
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 100005, maxm = 0;
char s[maxn];
vector<set<int> > v(26); //26个vector数组
//26个vector数组用于记录每个字母变量出现的位置,每个vector数组是一个set
set<int>::iterator it; //迭代器
int main()
{
scanf("%s", s + 1);
int i, j, q, len;
len = strlen(s + 1);
for (i = 1; i <= len; i++)
{
v[s[i] - 'a'].insert(i);
}
scanf("%d", &q);
while (q--)
{
int op;
scanf("%d", &op);
if (op == 1)
{
int pos;
char c[5];
scanf("%d", &pos);
scanf("%s", c);
v[s[pos] - 'a'].erase(pos);
s[pos] = c[0];
v[c[0] - 'a'].insert(pos);
// printf("%s\n",s);
}
else
{
int l, r, ans = 0;
scanf("%d%d", &l, &r);
for (i = 0; i < 26; i++)
{
it = v[i].lower_bound(l);
//it = lower_bound(v[i].begin(),v[i].end(),l);
//注释写法会超时。。。(网上有解释记住上面的比较快就行了。..)
if (it != v[i].end() && *it <= r )
ans++;
}
printf("%d\n", ans);
}
}
}