ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Educational Codeforces Round 130 (Rated for Div. 2) C. awoo's Favorite Problem

2022-07-08 02:04:32  阅读:150  来源: 互联网

标签:Educational Rated cout int posb tt Favorite include sumc


https://codeforc.es/contest/1697/problem/C

因为规则中,两种字符串变换都与‘b’有关,所以我们根据b的位置来进行考虑;

先去掉所有的'b',如果两字符串不相等就“NO”;

否则通过‘b'在a,b串中的位置,如果posa>posb,那么他们之间如果出现'a'就说明不可能

如果posb<posa,那么他们之间如果出现'c'说明不可能

 1 # include<iostream>
 2 # include<bits/stdc++.h>
 3 using namespace std;
 4 const int N = 3e5 + 10;
 5 #define int long long
 6 # define endl "\n"
 7 int suma[N], sumc[N];
 8 char a[N], b[N];
 9 char newa[N], newb[N];
10 int posa[N], posb[N];
11 void solve() {
12     int n;
13     cin >> n;
14     cin >> a + 1 >> b + 1;
15     int cnt1 = 0, cnt2 = 0;
16     for (int i = 1; i <= n; ++i) {
17         if (a[i] == 'a') suma[i] = suma[i - 1] + 1;
18         else suma[i] = suma[i - 1];
19         if (a[i] == 'c') sumc[i] = sumc[i - 1] + 1;
20         else sumc[i] = sumc[i - 1];
21         if (a[i] != 'b') newa[++cnt1] = a[i];
22         if (b[i] != 'b') newb[++cnt2] = b[i];
23     }
24 
25     if (cnt1 != cnt2) {
26         cout << "NO" << endl;
27         return;
28     }
29     for (int i = 1; i <= cnt1; ++i) {
30         if (newa[i] != newb[i]) {
31             cout << "NO" << endl;
32             return;
33         }
34     }
35     cnt1 = 0, cnt2  = 0;
36     for (int i = 1; i <= n; ++i) {
37         if (a[i] == 'b') posa[++cnt1] = i;
38         if (b[i] == 'b') posb[++cnt2] = i;
39     }
40     if (cnt1 != cnt2) {
41         cout << "NO" << endl;
42         return;
43     }
44     for (int i = 1; i <= cnt1; ++i) {
45         int x = posa[i], y = posb[i];
46         if (x < y) {
47             if (suma[y] - suma[x - 1] != 0) /*前缀和判断a的位置*/
         { 48 cout << "NO" << endl; 49 return; 50 } 51 } 52 if (x > y) { 53 if (sumc[x] - sumc[y - 1] != 0) { 54 cout << "NO" << endl; 55 return; 56 } 57 } 58 59 } 60 cout << "YES" << endl; 61 } 62 int tt; 63 signed main() { 64 ios::sync_with_stdio(false); 65 cin.tie(0); 66 cout.tie(0); 67 68 cin >> tt; 69 70 while (tt--)solve(); 71 return 0; 72 } 73 /* 74 75 */

主要是通过前缀和来判断两个pos之间是否存在'a'或者'c'的操作

标签:Educational,Rated,cout,int,posb,tt,Favorite,include,sumc
来源: https://www.cnblogs.com/empty-y/p/16456813.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有