Atcoder beginer contest 237 (A--D) problem solving Report

Posted by nielsg on Wed, 02 Feb 2022 06:45:09 +0100

A - Not Overflow

Question meaning: judge whether an input number is between the 31st power of - 2 and the 31st power of - 1
Idea: note that the input range is actually to input a long long number and find out whether an input number is in the int range.
Simple questions can be judged directly

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long ans=0;
    cin>>ans;
    if(ans>=(-1)*quickpow(2,31)&&ans<=quickpow(2,31)-1)
    {
        scYes;
    }
    else
    {
        scNo;
    }
    return 0;
}

B - Matrix Transposition

Input a matrix and output the inverse of the matrix.
Idea: at first, I wanted to do it directly. I used to use array to simulate, but I found that the boundary would reach 1e5, and the classic opening was not enough, so I used vector array to simulate the output backwards.

#include<bits/stds++.h>
using namespace std;
const int maxn=1e5+100;
vector<int >a[maxn];
int main()
{
    int n,i,j,m;
    cin>>n>>m;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            int d1;
            cin>>d1;
            a[i].push_back(d1);
        }
    }
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            cout<<a[j][i]<<" ";
        }
        cout<<endl;
    }
    return 0;
}

C - kasaka

Question meaning: if you enter a string, you can only add a from the front to find out whether it is a complete palindrome string after adding.
Idea: at the beginning, I didn't see the scope clearly. I wanted to delete the character a from the front and back respectively, and then delete it through the position of the marking machine. I don't know what's wrong with this idea. I kept wa. After careful consideration, I found that it still needs to judge the two situations of ba and ab, and then it passed. In fact, it is judged that the number of broken leading a is greater than the current leading a, which is still too small.

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s1,s2;
    int flag1=0,flag2=0;
    cin>>s1;
    for(int i=0;i<s1.length()/2;i++)
    {
        if(s1[i]!=s1[s1.length()-i-1])
        {
            flag1=1;
        }
    }
    if(flag1==0)
    {
        scYes
    }
    else
    {
        int d1=s1.length();
        int d2=s1.length()/2;
        int i,f1=s1.length(),f2=0;
        int cnt1=0,cnt2=0;
        for(i=d1-1;i>0;i--)
        {
            if(s1[i]=='a') {cnt1++;continue;}
            else break;
        }
        f2=i;
        for(i=0;i<d1;i++)
        {
            if(s1[i]=='a') {cnt2++;continue;}
            else break;
        }
        f1=i;
        if(cnt2>=cnt1)
        {
            scNo;
            return 0;
        }
        for(int i=f1;i<=f2;i++)
        {
            s2+=s1[i];
        }
        int ff=0;
        for(int i=0;i<s2.length()/2;i++)
        {
            if(s2[i]!=s2[s2.length()-i-1])
            {
                ff=1;
            }
        }
        if(ff==0)
        {
            scYes
        }
        else
        {
            scNo;
        }
    }
    return 0;
}

D - LR insertion

Give you n+1 numbers, and then follow the requirements of the string it gives. For example, if you give you a string that is
"LR", then the first step is to add a 1 to the left of 0, and then add a 2 from the right of 1, so the final result is 120
Idea: at first, I wanted to do it with data structures such as queues and stacks. It seemed that they couldn't solve this problem. Then I forgot about the linked list. As a result, I found that they actually said it was a double ended queue??? WTF, after thinking for a long time, I found it impossible. As a result, the first step of looking at other people's code reverses the string, so here we need a reverse idea. If you reverse all operations, you will find that this is a naked double ended queue, but I still didn't find out how they came up with it...

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i,j,t;
    string s1;
    deque<int >d1;
    cin>>n>>s1;
    reverse(s1.begin(),s1.end());
    d1.push_back(n);
    n-=1;
    for(i=0;i<s1.length();i++)
    {
        if(s1[i]=='L')
        {
            d1.push_front(n);
        }
        else
        {
            d1.push_back(n);
        }
        n-=1;
    }
    int length=d1.size();
    reverse(d1.begin(),d1.end());
    for(i=0;i<length;i++)
    {
        cout<<d1.front()<<" ";
        d1.pop_front();
    }
 
    return 0;
}

Topics: Algorithm atcoder