zoukankan      html  css  js  c++  java
  • 题解【Codeforces1234D】Distinct Characters Queries

    题面

    考虑使用 (26) 个 set 容器存储每一个字母在字符串中出现的位置。

    对于操作一,先把原字符串中的字母删去,修改那一个字母,然后再添加进去就可以了。

    对于操作二,枚举每一个字母,二分找到它在原串中出现的第一个大于等于 (l) 的位置,如果它在 ([l, r]) 区间内,答案就 (+1)。二分的过程可以使用 STL 中的 ( ext{lower})_( ext{bound}) 实现。

    代码比较简单。

    #include <bits/stdc++.h>
    #define DEBUG fprintf(stderr, "Passing [%s] line %d
    ", __FUNCTION__, __LINE__)
    #define File(x) freopen(x".in","r",stdin); freopen(x".out","w",stdout)
    
    using namespace std;
    
    typedef long long LL;
    typedef pair <int, int> PII;
    typedef pair <int, PII> PIII;
    
    inline int gi()
    {
    	int f = 1, x = 0; char c = getchar();
    	while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
    	while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    	return f * x;
    }
    
    inline LL gl()
    {
    	LL f = 1, x = 0; char c = getchar();
    	while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
    	while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    	return f * x;
    }
    
    const int INF = 0x3f3f3f3f, N = 200003;
    
    int n, q;
    char s[N];
    set <int> t[30];
    
    int main()
    {
    	//File("");
    	scanf("%s", s + 1);
    	for (int i = 1; i <= strlen(s + 1); i+=1) t[s[i] - 'a'].insert(i); //先将原串中的字母加入 set
    	q = gi();
    	while (q--)
    	{
    		int op = gi();
    		if (op == 1)
    		{
    			int l; char c[2];
    			scanf("%d%s", &l, c);
    			t[s[l] - 'a'].erase(l); //删去原字母
    			s[l] = c[0]; //修改
    			t[c[0] - 'a'].insert(l); //添加新字母
    		}
    		else 
    		{
    			int l = gi(), r = gi();
    			int ans = 0;
    			for (int i = 0; i < 26; i+=1) //枚举每一个字母
    			{
    				auto x = t[i].lower_bound(l); //使用 STL 的 lower_bound 二分
    				if (x != t[i].end() && *x <= r) ++ans; //在合法区间内就统计答案
    			}
    			cout << ans << endl;
    		}
    	}
    	return 0;
    }
    

    其实有些题目看到这种需要求出区间内出现过的 字母 / 数字 的问题时,在 字母 / 数字 的范围较小时,可以考虑枚举每一个 字母 / 数字,判断它是否在给定区间内出现过,这个过程可以像本本题一样使用 STL set 来实现。

  • 相关阅读:
    [Shoi2007]Vote 善意的投票
    [CQOI2015]任务查询系统
    [SDOI2013]森林
    HttpClient 教程 (二)
    /system改成可写
    Netbeans 6.8 + apktool_2.0.0b9 动态调试smali文件
    把中文版NetBeans改成英文版
    ZjDroid工具介绍及脱壳详细示例
    grep过滤搜索
    android Log图文详解(Log.v,Log.d,Log.i,Log.w,Log.e)
  • 原文地址:https://www.cnblogs.com/xsl19/p/12521123.html
Copyright © 2011-2022 走看看