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 ...
随机推荐
- 如果在Yii中,使用AR查询,不直接写sql,则在使用的时候会报错
如果在Yii中,使用AR查询,不直接写sql,则在使用的时候会报错 Student::find() ->select("id,name,from_unixtime(create_tim ...
- 了解JWT认证
JWT介绍 JWT的全称为Json web token,是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准((RFC 7519).该token被设计为紧凑且安全的,特别适用于分布式站点的 ...
- uniapp微信小程序canvas隐藏
HTML 我是把canvas嵌套在view里并被view设置id <view id="canvas"> <canvas style="width: 35 ...
- Servlet学习笔记(二)
目录 Servlet学习笔记(二) Request对象 1.request和response对象: 2.request对象继承体系结构: 3.什么是HttpServletRequest ? 4.Htt ...
- SpringBoot的外部化配置最全解析!
目录 SpringBoot中的配置解析[Externalized Configuration] 本篇要点 一.SpringBoot官方文档对于外部化配置的介绍及作用顺序 二.各种外部化配置举例 1.随 ...
- wpf 全局异常捕捉+简单日志记录
`namespace MyApp { /// /// App.xaml 的交互逻辑 /// public partial class App : Application { public App() ...
- linux 源码下载和在线查看网站
下载: https://www.kernel.org/ 查看: https://elixir.bootlin.com/linux/
- UNP——第二章,常见协议概述
1.为什么要了解协议 程序员与协议合作,完成应用. 了解协议是为了了解协议完成了什么,提供了什么服务,自己还应该做什么. 2.从协议的角度,套接字是什么 套接字是协议的接口, IP套接字,代表可使用I ...
- Qt For MacOs环境搭建
使用VMWARE关于macos镜像搭建,参考https://blog.csdn.net/u011415782/article/details/78505422 关于darwin8.5.5 来安装vmt ...
- Java并发编程 - Runnbale、Future、Callable 你不知道的那点事(二)
Java并发编程 - Runnbale.Future.Callable 你不知道的那点事(一)大致说明了一下 Runnable.Future.Callable 接口之间的关系,也说明了一些内部常用的方 ...