B. Irreducible Anagrams【CF 1290B】


思路:

设tx为t类别字符的个数。
①对于长度小于2的t明显是"YES"
②对于字符类别只有1个的t明显是"YES"
③对于字符类别有2个的t,如左上图:如果str[l] != str[r],那么我们构造的t也应该是str[l] != str[r],且s字串和t的str[l]和str[r]是相反的,即如图所示。继续,如图构造,即bbb..a...a这样,我们发现第一个图片除去str[l] = a和str[r]=b之外,中间怎么放置字符,都会出现"Irreducible Anagrams"的情况,所以"YES"。
④对于字符类别有2个的t,如果str[l] == str[r],如右边的图,总有k = 2,让s1包含一个a和bx个b,使得"reducible Anagrams"存在,所以"NO"。
④对于字符类别有3个的t,按着左上的图也无法构造出"Irreducible Anagrams" 情况,说明字符类别为3的t,不论说明字符排列都存在"reducible Anagrams",所以"NO"。
⑤对于字符类别大于3个的t,由④推出是"NO"。
1 #include <iostream>
2 #include <cstdio>
3 #include <algorithm>
4 #include <cstring>
5 using namespace std;
6
7 const int N = 2e5 + 10;
8 int dp[30][N];
9 char str[N];
10
11 void solve()
12 {
13 scanf("%s", str);
14 int n = strlen(str);
15 /// cout << "n = " << n << endl;
16 for(int i = 1; i <= n; ++i) {
17 dp[str[i - 1] - 'a'][i]++;
18 /// cout << dp[str[i - 1] - 'a'][i] << endl;
19 for(int c = 0; c < 26; ++c) {
20 dp[c][i] += dp[c][i - 1];
21 }
22 }
23 /*
24 for(int c = 0; c < 26; ++c) {
25 printf("%c :\n", 'a' + c);
26 for(int i = 1; i <= n; ++i) {
27 printf("%d ", dp[c][i]);
28 }
29 printf("\n");
30 }
31 */
32 int q;
33 scanf("%d", &q);
34 vector<pair<int ,int > > vp;
35 for(int i = 0; i < q; ++i) {
36 int l, r;
37 scanf("%d%d", &l, &r);
38 vp.push_back(make_pair(l, r));
39 }
40
41 ///vector<int > ans;
42 for(auto info : vp) {
43 int l = info.first;
44 int r = info.second;
45
46 int kinds = 0;
47 int sum = 0;
48 for(int c = 0; c < 26; ++c) {
49 kinds += (dp[c][r] - dp[c][l - 1]) > 0;
50 sum += dp[c][r] - dp[c][l - 1];
51 }
52 ///cout << "tot = " << kinds << endl;
53 if(sum == 1 || (kinds == 2 && str[l - 1] != str[r - 1]) || kinds > 2) {
54 printf("YES\n");
55 } else printf("NO\n");
56 }
57
58 }
59
60 int main()
61 {
62 solve();
63
64 return 0;
65 }
B. Irreducible Anagrams【CF 1290B】的更多相关文章
- 【CF#338D】GCD Table
[题目描述] 有一张N,M<=10^12的表格,i行j列的元素是gcd(i,j) 读入一个长度不超过10^4,元素不超过10^12的序列a[1..k],问是否在某一行中出现过 [题解] 要保证g ...
- 【CF#303D】Rotatable Number
[题目描述] Bike是一位机智的少年,非常喜欢数学.他受到142857的启发,发明了一种叫做“循环数”的数. 如你所见,142857是一个神奇的数字,因为它的所有循环排列能由它乘以1,2,...,6 ...
- 【CF 463F】Escape Through Leaf
题意 给你一棵 \(n\) 个点的树,每个节点有两个权值 \(a_i,b_i\). 从一个点 \(u\) 可以跳到以其为根的子树内的任意一点 \(v\)(不能跳到 \(u\) 自己),代价是 \(a_ ...
- 【CF 453A】 A. Little Pony and Expected Maximum(期望、快速幂)
A. Little Pony and Expected Maximum time limit per test 1 second memory limit per test 256 megabytes ...
- 【CF 585E】 E. Present for Vitalik the Philatelist
E. Present for Vitalik the Philatelist time limit per test 5 seconds memory limit per test 256 megab ...
- 【35.20%】【CF 706D】Vasiliy's Multiset
time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standa ...
- 【26.8%】【CF 46D】Parking Lot
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 【31.42%】【CF 714A】Meeting of Old Friends
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- 【31.95%】【CF 714B】Filya and Homework
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
随机推荐
- Java中的(String args[])
1. DOS下运行 首先,String args[] 这个形式可以直接看出它就是一个字符串数组充当main函数形式参数,args是arguments的缩写,不是关键字(就是一个数组名),可以改但没必要 ...
- 不吹不黑,跨平台框架AspNetCore开发实践杂谈
前言 最近边学边做,初步上手了AspNetCore的开发,对MVC这套熟悉了一下,因为之前没有拿来做过独立项目,都是和别人合作开发,所以前后端分离,我都只做WebApi,而且还是很小的项目(课设级别) ...
- 不断要求用户输入学生姓名,输入q结束.
while (true) { Console.WriteLine("请输入学生姓名"); string a = Console.ReadLine(); if (a == " ...
- 鸿蒙开发板外设控制 之 实现物理按键的“长按事件”(按键通用框架 V0.0.2)
我在之前的帖子<实现按键"按下事件"和"释放事件"的通用框架(V0.0.1)>中阐述了DTButton-V0.0.1的设计思路,并且也在帖子中开源了 ...
- 谈谈volatile
volatile的作用: volatile关键字的作用包括:保障可见性,保障有序性. 何为保障可见性,看下面的代码: package com.mashibing.thread.lock; public ...
- centos常用指令之-卸载
卸载centos自带java: rpm -qa|grep java // 查询javax相关 xxxxxxxxxxxxxx # 卸载1.2方式 # 1 yum -y remove java xxxxx ...
- 强迫自己学Jquery
Jquery 适合我吗? 里面有很多用不上的功能, 我需要什么,常用的判断,常用的扩展,来自Dojo的Hitch函数,事件绑定,样式处理(添加 删除 替换),Ajax,模块加载, 足够了,真心不想用这 ...
- dp背包 面试题 08.11. 硬币
https://leetcode-cn.com/problems/coin-lcci/ 硬币.给定数量不限的硬币,币值为25分.10分.5分和1分,编写代码计算n分有几种表示法.(结果可能会很大,你需 ...
- JAVA程序员工作常用英语(细心整理)
基础----进阶 A. array数组accessible 可存取的 area面积audio 音频 addition 加法 action 行动 arithmetic 算法adjustment 调整 a ...
- 学习一下 Spring Security
一.Spring Security 1.什么是 Spring Security? (1)基本认识 Spring Security 是基于 Spring 框架,用于解决 Web 应用安全性的 一种方案, ...