Codeforces Round 918 (Div. 4)部分补题D-F

文章描述了两道C++编程题目的解决方案:一道是将给定字符串按照CVC的音节格式分割并输出,另一道是检查一串数字中是否存在偶数位置和奇数位置之和相等的情况。
摘要由CSDN通过智能技术生成

Problem - E - Codeforces

题目大意:将字符串划分为CV与CVC的音节格式,中间由","分割   C(a,e) ,V(b,c,d)

#include <bits/stdc++.h>
using namespace std;

const int N = 4e5 + 5;
int c[N];//标记是否为C
char d[2 * N];//存储分割后要输出的字符
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int T;
    cin >> T;
    while (T--)
    {
        int n;
        cin >> n;
        string s;
        cin >> s;
        int t = 0;
        memset(c, 0, sizeof(c));//每次数组记得清空
        memset(d, 0, sizeof(d));
        for (int i = 0; i < n; i++)
        {
            if (s[i] == 'a' || s[i] == 'e')
            {
                c[i] = 1;
            }
            else
            {
                c[i] = 0;
            }
        }
        for (int i = n - 1; i >= 0; i--)//倒叙来判断分割cv cvc,由于顺序遍历时c相同不好判断
        {
            if (c[i] == 1)//遇到是v的后面加"."
            {
                d[t++] = s[i--];//i--是在存储完这个数组后的数据后才i--运行的
                d[t++] = s[i];
                d[t++] = '.';
            }
            else
            {
                d[t++] = s[i--];
                d[t++] = s[i--];
                d[t++] = s[i];
                d[t++] = '.';
            }
        }
        for (int i = t - 2; i >= 0; i--)//记得最后第一个是".",要删去
        {
            cout << d[i];
        }
        cout << endl;
    }
    return 0;
}

E

题目大意:

偶数上的数相加等于奇数上的数相加,在一串数字的串中的l,r中找到此类情况,则输出YES,反之,输出NO;

#include <bits/stdc++.h>
using namespace std;
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        long long s = 0;
        map<long long, int> mp;
        string ans = "NO";
        for (int i = 0; i < n; i++)
        {
            int x;
            cin >> x;
            if (i %2 ==0)
                s += x;
            else
                s -= x;
            mp[s]++;
            if (mp[s] > 1 || s == 0)//s==0表示总和相等,s在mp出现的个数不止一次表示,说明有一段是重复后抵消完的
            {
                ans = "YES";
            }
        }
        cout << ans << endl;
    }
    
    return 0;
}

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值