codeforcesDiv2AB题解

打过的最难的一场div2 只出了两题居然还上分了

A 给你一个串S其中只含有abc?四种字符,然后可以替换成任意一个小写字母,问你是否S字符串是否可以只包含一个b串abacabaabacabacaba算包含两个b串

A题这个难度确实吓到我了,虽然测试用例很多,但是S的长度不大于50,于是想着暴力试一下,瞎搞了一下,AC了。

思路:先进行判断S中已经包含几个b串

  • >1那么直接返回不行,
  • =1那么直接把问号全变成abc以外的字母,防止出现新的b串
  • =0那么进行暴力匹配,遇到?默认相等,如果能匹配成功,进行替换,但是问题是,替换成功之后可能不止一个b串,例如abacab?bacaba,暴力匹配成功之后把替换成a,这样后面也满足了,因此替换之后再检查一遍,如果仅有一串,那么满足条件,否则继续匹配后面的。

AC代码:

/*
 * @Author: hesorchen
 * @Date: 2020-07-03 17:05:01
 * @LastEditTime: 2020-07-19 17:33:32
 * @Description: https://hesorchen.github.io/
 */
#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <cmath>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define endl '\n'
#define PI acos(-1)
#define PB push_back
#define ll long long
#define INF 0x3f3f3f3f
#define mod 1000000007
#define pll pair<ll, ll>
#define lowbit(abcd) (abcd & (-abcd))
#define max(a, b) ((a > b) ? (a) : (b))
#define min(a, b) ((a < b) ? (a) : (b))

#define IOS                      \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);
#define FRE                              \
    {                                    \
        freopen("in.txt", "r", stdin);   \
        freopen("out.txt", "w", stdout); \
    }

inline ll read()
{
    ll x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return x * f;
}
//head==============================================================================

string a;
string b = "abacaba";
string temp;
ll len;

int check() //暴力判断temp中含有几个b
{
    ll res = 0;
    for (int i = 7; i <= len; i++)
    {
        if (temp.substr(i - 7, 7) == b)
            res++;
    }
    return res;
}

int main()
{

    IOS;
    ll t;
    cin >> t;
    while (t--)
    {
        cin >> len;
        cin >> a;
        temp = a;
        if (len < 7)
        {
            cout << "No\n";
            continue;
        }

        ll sum = check();
        if (sum >= 2)
        {
            cout << "No\n";
            continue;
        }
        else if (sum == 1)
        {
            for (int i = 0; i < len; i++)
            {
                if (a[i] == '?')
                    a[i] = 'z';
            }
            cout << "Yes\n";
            cout << a << endl;
            continue;
        }
        ll ans = 0;
        for (int i = 0; i <= len - 7; i++)
        {
            ll flag = 1;
            for (int j = i; j < i + 7; j++)
                if (a[j] == b[j - i] || a[j] == '?')
                    continue;
                else
                    flag = 0;
            if (flag)
            {
                temp = a;
                for (int j = i; j < i + 7; j++)
                    temp[j] = b[j - i];
                if (check() == 1)
                {
                    ans = 1;
                    break;
                }
            }
        }
        if (ans)
        {
            for (int i = 0; i < len; i++)
            {
                if (temp[i] == '?')
                    temp[i] = 'z';
            }
            cout << "Yes\n";
            cout << temp << endl;
        }
        else
            cout << "No\n";
    }

    return 0;
}

/*
1 19
abacab?bacababacab?


*/

B 给你一个区间[l,r],然后再给你一个正整数m,让你求出一个三元组a,b,c,abc均在区间[l,r]中,满足n=(m-b+c)/a,n是正整数

我的思路就是枚举a,然后找满足条件的b和c,二分b进行判断,也是瞎搞搞出来的。

/*
 * @Author: hesorchen
 * @Date: 2020-07-03 17:05:01
 * @LastEditTime: 2020-07-19 18:25:15
 * @Description: https://hesorchen.github.io/
 */
#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <cmath>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define endl '\n'
#define PI acos(-1)
#define PB push_back
#define ll long long
#define INF 0x3f3f3f3f
#define mod 1000000007
#define pll pair<ll, ll>
#define lowbit(abcd) (abcd & (-abcd))
#define max(a, b) ((a > b) ? (a) : (b))
#define min(a, b) ((a < b) ? (a) : (b))

#define IOS                      \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);
#define FRE                              \
    {                                    \
        freopen("in.txt", "r", stdin);   \
        freopen("out.txt", "w", stdout); \
    }

inline ll read()
{
    ll x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return x * f;
}
//head==============================================================================

int main()
{
    ll t;
    cin >> t;
    while (t--)
    {
        ll l, r, m;
        cin >> l >> r >> m;
        ll flag = 0;
        for (int i = l; i <= r; i++)
        {
            ll temp1 = m + l - r, temp2 = m + r - l;
            temp1 = max(1, temp1);
            if (temp1 % i == 0)
            {
                cout << i << ' ' << r << ' ' << l << endl;
                // cout << 111 << endl;
                flag = 1;
            }
            else if (temp2 % i == 0)
            {
                cout << i << ' ' << l << ' ' << r << endl;
                // cout << 222 << endl;
                flag = 1;
            }
            else if (temp2 / i - temp1 / i >= 1 && temp2 >= 0 && temp1 >= 0)
            {
                ll res = (temp1 / i + 1) * i - m; //c-b
                // cout << "res=" << ' ' << res << endl;
                ll left = l, right = r;
                while (left <= right) //二分 b
                {
                    // cout << left << ' ' << right << ' ' << res << ' ' << i << endl;
                    // cout << 1 << endl;
                    ll mid = left + right >> 1;
                    if (mid + res <= r && mid + res >= l)
                    {
                        cout << i << ' ' << mid << ' ' << mid + res << endl;
                        flag = 1;
                        // cout << 333 << endl;
                        break;
                    }
                    if (mid + res > r)
                    {
                        right = mid - 1;
                    }
                    else if (mid + res < l)
                    {
                        left = mid + 1;
                    }
                }
            }
            if (flag)
                break;
        }
    }
    return 0;
}

/*

1
2 10 5

*/
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hesorchen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值