AcWing周赛47反思总结

本文介绍了两道AcWing在线编程题目,第一题通过遍历字符串并使用unordered_set存储非特殊字符来求解;第二题利用队列进行元素旋转,通过a%q.size()减少循环次数,实现高效运算。两题均展示了简单的算法应用。
摘要由CSDN通过智能技术生成

一、AcWing 4399. 数字母

1、原题链接:AcWing 4399. 数字母 

2、解题思路:

直接暴力干就完了!

3、参考代码:

#include<bits/stdc++.h>
using namespace std;
string s;
unordered_set<char> t;
int main(){
    getline(cin,s);
    int n = s.size();
    for (int i = 0; i < n; i ++ ){
        if(s[i]!='{' && s[i] != '}' && s[i] != ',' && s[i] != ' '){
            t.insert(s[i]);
        }
    }
    cout << t.size();
    return 0;
    
}

二、4400. 玩游戏

1、原题链接4400. 玩游戏

2、解题思路:

本题就是用一个队列来存放前n个数,然后将队头放到队尾,且删除现队头,完成队列的后移,为了避免循环次数变多,可以让a %= q.size()来减少循环次数,队列完成后移后,再先输出后删除队头元素即可。

3、参考代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

const int N = 110;

int n,m;

int main()
{
    cin >> n >> m;
    
    queue<int> q;
    
    for(int i = 1; i <= n; i ++ ) q.push(i);
    
    while( m -- ){
        int a;
        cin >> a;
        //减少循环次数
        a %= q.size();
        
        for(int i = 0; i < a; i ++){
            q.push(q.front());
            q.pop();
        }
        
        cout << q.front() << ' ';
        q.pop();
    }
    return 0;
}

三、AcWing 4401. 找回数组 

1、原题链接:AcWing 4401. 找回数组 

2、解题思路:

首先转换一下题中的式子:x[i]%k = a[i+1] -a[i]。若存在a[i] - a[i-1] != a[i-k] - a[i - k -1]则表示当前k值存在矛盾,应舍去。最后输出vector<int> res的大小以及其中的元素即可。

3、参考代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

const int N = 1010;
int a[N];
int n;

int main()
{
    cin >> n;
    for (int i = 1; i <= n; i ++ ) cin >> a[i];
    
    vector<int> res;
    
    for(int k = 1; k <= n; k ++){
        bool is_match = true;
        for(int i = k + 1; i <= n; i ++){
            if(a[i] - a[i-1] != a[i-k] - a[i - k -1]){
                is_match = false;
                break;
            }
        }
         if(is_match) res.push_back(k);
    }
    
    cout << res.size() << endl;
    for(auto k : res) cout << k << ' ';
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值