codeforces1234 D - Distinct Characters Queries [set解法]

题意:

给你一个字符串下面你的操作

op = 1,将pos位的字母换成x

op = 2,问你l~r之间有多少个不同的字符串

解法

set数组 神奇如斯!!

26个set set里放字母位置

找到在y[i]这个set中比l大于等于的第一个地址 ->神奇二分恐怖如斯!!

线段树也可解 下一篇写qaq

AC代码

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<set>
#include<cstring>
#include<string>

using namespace std;
const int MaxN = 2e5 + 5;

int n,op,l,r,pos;
char x;
char s[MaxN];
set<int> y[30];

int main()
{
	scanf("%s",s + 1);
	for(int i = 1;i <= strlen(s + 1); i++){
		y[s[i] - 'a'].insert(i);
	}
	scanf("%d",&n);
	for(int i = 1;i <= n; i++){
		scanf("%d",&op);
		if(op == 1){
			scanf("%d %c",&pos,&x);
			y[s[pos] - 'a'].erase(pos);
			s[pos] = x;
			y[x - 'a'].insert(pos);
		}
		else{
			int ans = 0;
			scanf("%d %d",&l,&r);
			for(int j = 0;j < 26; j++){
				if(y[j].size() == 0) continue;
				set<int> :: iterator it = y[j].lower_bound(l);
				if(it != y[j].end() && *it <= r) ans++;
			}
			printf("%d\n",ans);
		}
	}
}
发布了31 篇原创文章 · 获赞 5 · 访问量 1376

猜你喜欢

转载自blog.csdn.net/qq_43685900/article/details/102020833