CS Academy Round #49 A, B, C

A.Odd Sum

题目链接

https://csacademy.com/contest/round-49/task/odd-sum/statement/

题解

给组数让求其中两两组合可以形成奇数的个数
直接统计奇数偶数个数相乘

AC代码

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

#define LL long long
#define CLR(a,b) memset(a,(b),sizeof(a))

const int MAXM = 1e3+10;
const int MAXN = 1e5+10;
const int mod = 1e9+7;
int arr[MAXN];
int main()
{
    int n;
    scanf("%d",&n);
    int x;
    int k2 = 0, k1 = 0;
    for(int i = 0;i < n; i++) {
        scanf("%d",&x);
        if(x&1) k1++;
        else k2++;
    }
    printf("%d\n",k1*k2);

return 0;
}

B.Money Machine

题目链接

https://csacademy.com/contest/round-49/task/money-machine/

题解

很裸的贪心

AC代码

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

#define LL long long
#define CLR(a,b) memset(a,(b),sizeof(a))

const int MAXM = 1e3+10;
const int MAXN = 1e5+10;
const int mod = 1e9+7;
int arr[MAXN];
int main()
{
    int n, c, x1, u, x2;
    scanf("%d%d%d%d%d",&n,&c,&x1,&u,&x2);
    int ans = 0;
    for(int i = 1; i <= n; i++) {
        if(c>=u && u < (n-i+1)*x2) {
            c -= u; ans += x2;
            //printf("%d %d %d\n",c,u,ans);
        }
        c = c + x1 + ans;
    }
    printf("%d\n",c);

return 0;
}

C.Max Substring

题目链接

https://csacademy.com/contest/round-49/task/max-substring/

题解

英语是硬伤啊,硬是看成了暴力KMP,字符串问题

时间复杂度 O(n26)
判断
str[pos[i][1]+L]=str[pos[i][2]+L]=str[pos[i][3]+L]=....=str[pos[i][N]+L] 在字符串中的位置其中 L 为合适的字串大小
pos[i][1] 为i字符在主串中的位置1,2,3。。。

AC代码

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

#define LL long long
#define CLR(a,b) memset(a,(b),sizeof(a))

const int MAXM = 1e3+10;
const int MAXN = 1e5*4;
const int mod = 1e9+7;
string str;
vector<int> pos[26];
int main()
{
    cin>>str;
    int len = str.size();
    // get the positions for all charactes
    for(int i = 0; i < len; i++) {
        pos[str[i]-'a'].push_back(i);
    }
    // get the max numgber of ocueences of a character
    int maxx = 0;
    for(int i = 0; i < 26; i++) {
        maxx = max(maxx,(int)pos[i].size());
    }
    string mx_answer = "";
    for(int i = 0; i < 26; i++) {
        int sz = 1;
        if((int)pos[i].size() != maxx) continue;
        while(pos[i].back()+sz < len) {
            char ch = str[pos[i][0]+sz];
            bool flag = true;
            //for(auto it = pos[i].begin(); it != pos[i].end(); it++)
            for(auto it : pos[i]) {
                if(str[it+sz] != ch) flag = false;
            }

            // the end characters are not equal
            if(!flag) break;

            // all good ,continue trying to increase size
            sz++;
        }
        // actually compute a string of length 'size' to compare it with the max string
        string c_str = "";
        for(int j = 0; j < sz; j++) {

            c_str += str[pos[i][0]+j];
        }
        // if the size is bigger or the string is smaller lexicographically
        // update the answer
        if(sz>(int)mx_answer.size() || sz>c_str.size())
            mx_answer = c_str;
    }
    cout<<mx_answer<<'\n';
return 0;
}




















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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值