Weekend Summary 4 (Basic Common Knowledge)

Keywords: C++ Algorithm string

This week is also not much, no week I am satisfied with, no sense of knowledge to enrich, feel pushed by the task, plan the weekend two days to brush topics and organize evening homework, but really not as good as to do it in advance. On Sunday, I temporarily added two thought reports, a love letter, and I was very speechless. I hope I can be more efficient this week. Let me sort out some of the basic topics I've done this week. I can tell you that I've got a lot of food after I've done them.

Blog titles seem to be many, but they are all simple and easy to understand. Some ideas and implementation are relatively important.

String Operation

P5015 [NOIP2018 Popularity Group] Title Statistics

subject
1. Use of getline (cin, x)

Title: Calculate the number of characters except spaces and line breaks
Input:
234
Output:
3
Input:
Ca 45
Output:
4

#include <bits/stdc++.h>
using namespace std;
string s;
int main()
{
    getline(cin,s);
    int l=s.length();
    int ans=l;
    for(int i=0;i<l;i++)
    {
        if(s[i]==' ')
        {
            ans--;
        }
    }
    cout<<ans<<endl;
    return 0;
}

P1055 [NOIP2008 Popularization Group] ISBN Number

subject

1. Note the conversion of integers and characters
2. Learn how to optimize your code by putting the remainder in an array, using subscripts and equals the remainder

Calculation Rules: See topic

Input: 0-670-82162-4
Output:Right
Input: 0-670-82162-0
Output: 0-670-82162-4

#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    cin>>s;
    int j=1;
    int sum=0;
    int num=1;
    int ans=0;
    for(int i=0; i<=12; i++)
    {
        if(i==11)
        {
            ans=s[12]-'0';
            break;
        }
        if(s[i]!='-')
        {
            sum+=j*int(s[i]-'0');
            j++;
        }
    }
    sum=sum%11;
    if(sum==ans||(sum==10&&s[12]=='X')) cout<<"Right"<<endl;
    else
    {
        for(int i=0; i<=11; i++)
        {
            cout<<s[i];
        }
        if(sum==10) cout<<'X'<<endl;
        else   cout<<char('0'+sum)<<endl;
    }
    return 0;
}

Optimize:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    char a[14];
    char mod[12]="0123456789X";
    cin>>a;//Input string
    int i,j=1,sum=0;
    for(i=0; i<12; i++)
    {
        if(a[i]=='-') continue;
        sum+=(a[i]-'0')*j++;
    }
    if(mod[sum%11]==a[12]) cout<<"Right"<<endl;
    else
    {
        a[12]=mod[sum%11];
        cout<<a<<endl;
    }
  //  cout<<endl;
return 0;
}

P1308 NOIP2011 Popularity Group Statistics Words

subject

Use of C++ string
1. Character Matching: b.find(a)!=string::npos
2. Convert case: b[i]=tolower(b[i]);
(See code for details)

Topic:
Enter a word, and a line of articles, and output the number of words that appear and where they first appear
Output-1 without this word
Analysis: First convert it to lowercase; Find has not found return-1, if found to retain the initial subscript, and continue traversing to calculate the total number; (mainly the use of functions)
Input:
To
to be or not to be is a question
Output:
2 0

Input:
to
Did the Ottoman Empire lose its power at that time
Output:
-1

Even though this code hasn't passed yet, 40 scores
But I'm not going to turn it up at night (I'm a little Raji)

#include <bits/stdc++.h>
using namespace std;
//Knowledge Points:
//Character Matching
//Convert case
int main()
{
    string a,b;
    getline(cin,a);
    getline(cin,b);
    a=' '+a+' ';
    b=' '+b+' ';
    for(int i=0;i<b.length();i++)
    {
        a[i]=tolower(a[i]);
    }
    for(int i=0;i<b.length();i++)
    {
        b[i]=tolower(b[i]);
    }
    //Convert to lowercase
    //Find if string a contains substring b
    //string::npos is used to indicate a location that does not exist
    if(b.find(a)==string::npos)
    {
        cout<<-1<<endl;
    }//Can't find
    else
    {
        int chushi=b.find(a);//Initial Subscript
        int n=b.find(a),sum=0;
        while(n!=string::npos)
        {
            ++sum;
            n=b.find(a,n+1);//Search from the next location
        }
        cout<<sum<<" "<<chushi<<endl;
    }
return 0;
}

P2010 [NOIP2016 Popularity Group] Palindrome Date

subject

An optimized solution must have been thought for a long time, so don't complain, or practice a little more and accumulate some ideas
Topic:
Find palindrome strings between two dates
Analysis:
Direct, simple and rough traversal judgment must have timed out. It's cruel.
The idea of this problem is to use the symmetry of the front and back strings of the palindrome string to calculate all the palindrome strings corresponding to 12 months (because the month and date have been determined, so the palindrome string will be unique), and then determine if the calculated palindrome string is between the two years given by the topic.

#include <iostream>

using namespace std;

int s[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
//0229 Reversed 9220 is a leap year
int main()
{
    int n,m,c,sum=0;
    int ans=0;
    cin>>n>>m;
    for(int i=0;i<=12;i++)//Enumerate months and days
    {
        for(int j=1;j<=s[i];j++)
        {
            c=(j%10)*1000+
            (j/10*100)+
            (i%10*10)+
            (i/10);//Calculate the top four digits
            sum=c*10000+i*100+j;//Calculate the entire date
            if(sum<n||sum>m) continue;
            ans++;
        }
    }
    cout<<ans<<endl;
    return 0;
}

P1012 [NOIP1998 Increase Group] Pin

subject
Topic:
Pin, the largest number is more or less not
Analysis:
At first I thought that the spelling would compare the highest position to the lower position, but I didn't think that one of them would end up comparing it to the highest position of the other to see who could put it first. For example, to understand this situation, 32 would take precedence over 321 because 32321 > 32132, but we generally think that 321 > 32 is the most common string comparison. So take this into account ~

Lower-level code, commonly referred to as more detailed code:

#include <iostream>
#define ll long long
using namespace std;
int t;
const int N=1e9;
bool cmp(int x,int y)
{
    return x>y;
}
//I'm thinking about storing numbers
//however
string a[30];
//string storage is more convenient (you can compare sizes directly)
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    for(int i=0;i<n;i++)//sort
    {
        for(int j=i+1;j<=n;j++)
        {
            if(a[j]+a[i]>a[i]+a[j])
            {
                swap(a[j],a[i]);
                /*
                Example 12, 34
                1234<3412
                12 And 34 need to exchange
                */
            }
        }
    }//End of Sort
    //Because the number of data is small, try writing a double loop to familiarize yourself with the principles
    for(int i=0;i<=n;i++)
    {
        cout<<a[i];
    }
    return 0;
}

Notice the definition of the comparison function cmp(), which is clever:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int t;
const int N=1e9;
bool cmp(string a,string b)
{
    return a+b>b+a;
}
//If return a>b
//321>32 will occur
string a[30];
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n,cmp);
    for(int i=0;i<n;i++)
    {
        cout<<a[i];
    }
    return 0;
}

P5587 Typing Practice

subject

The Chinese title, which is easy to understand, should not be repeated

Analysis:
1. Capture n lines of articles, use getline(cin, s), and count
2. Don't forget to consider the symbol'<'in the original text.
3. Delete is a counting problem. Considering that the symbol'<'begins with a deletion of itself, if it does not start with the first position at the beginning, in this case j-1>=0, delete itself and the previous element. Because the last input is to be compared with the original text, a backspace'<' deletes the previous element.
4. Precision handling, when encountering this requirement "Please calculate his KPM (Keys per minutes, the number of characters entered per minute), the answer is rounded to keep the integer part."

Look at the following:

    //Processing of Accuracy
    double k=sum;
    k=k*60/t;
    sum=k;//sum is of type int
    if(k-sum>0.5) sum++;//Enter One
    cout<<sum<<endl;
    return 0;

Complete dama:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e3+10;

int main()
{
    string s[N];
    string ss[N];

    int ii=0,jj=0;
    getline(cin,(s[ii]));
    while(s[ii]!="EOF")
    {
        ii++;
        getline(cin,(s[ii]));
    }
    getline(cin,(ss[jj]));
    while(ss[jj]!="EOF")
    {
        jj++;
        getline(cin,(ss[jj]));
    }
    int t;
    cin>>t;
    int sum=0;
    for(int i=0; i<ii; i++)
    {
        for(int j=0; j<s[i].length(); j++)
            if(s[i][j]=='<')
                if(j-1>=0)
                {
                    s[i].erase(j,1);
                    s[i].erase(j-1,1),j-=2;
                }
                else  s[i].erase(j,1),j--;
    }
    for(int i=0; i<ii; i++)
    {
        for(int j=0; j<ss[i].length(); j++)
            if(ss[i][j]=='<')
                if(j-1>=0)
                {
                    ss[i].erase(j,1);
                    ss[i].erase(j-1,1),j-=2;
                }
                else  ss[i].erase(j,1),j--;
    }
//    for(int i=0; i<ii; i++)
//    {
//        cout<<ss[i]<<endl;
//    }
    for(int i=0;i<min(ii,jj);i++)
    {
        for(int j=0;j<min(s[i].length(),ss[i].length());j++)
        {
            if(s[i][j]==ss[i][j])
                sum++;
        }
    }
    //Precision processing, no precision processing only 10 points
    double k=sum;
    k=k*60/t;
    sum=k;
    if(k-sum>0.5) sum++;
    cout<<sum<<endl;
    return 0;
}

Functions, Recursion and Recursion

Calculation of P1028 [NOIP2001 Pervasive Group]

subject
Topic:
Give a number n, precede it with a number not exceeding half of the first number, until no number can be added, and ask how many numbers satisfy the title.
6, 6, 16, 26, 126, 36, 136, output 6
Analysis:
Write and find the rules. Simply put, the number of species f[6] of 6 is the number of species f[1]+2, f[2]+3, f[3]

#include <bits/stdc++.h>
using namespace std;
const int N=1000;

int f[N];;
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=i/2;j++)
        {
            f[i]=f[i]+f[j];
        }
        f[i]++;
    }
//    for(int i=1;i<=n;i++)
//    {
//        cout<<f[i]<<" ";
//    }
    cout<<f[n]<<endl;
    return 0;
}

P1036 [NOIP2002 Pervasion Group] Selection

Title

Topic:
When k numbers are added together as prime numbers in n numbers, the output has several cases
Analysis:
The emphasis is on the dfs() function. See code details, for recursive immaturity you can simulate once on paper
Not once, twice

#include <bits/stdc++.h>
using namespace std;
bool prime(int n)
{
    for(int i=2;i*i<=n;i++)
        if(n%i==0)
            return false;
        return true;
}
int n,k;
long long ans;
int a[25];
//start ensures that the order is ascending, because there may be duplicates in the order, so go ahead and repeat
void dfs(int m,int sum,int start)//We're looking for k numbers to add up in n numbers
{
    if(m==k)//The number k meets the requirement. If it is a prime number, the count will end if it is not a prime number
    {
        if(prime(sum))
            ans++;
        return ;
    }
    for(int i=start;i<n;i++)
    {
        dfs(m+1,sum+a[i],i+1);//i+1 is the number before the guarantee is not repeated
    }
    return ;
}
int main()
{
    cin>>n>>k;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    dfs(0,0,0);
    cout<<ans<<endl;
    return 0;
}

P1464 Function

subject

Pop

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll w(ll a,ll b,ll c)
{
    if(a<=0||b<=0||c<=0) return 1;
    else if(a>20||b>20||c>20) return w(20,20,20);
    else if(a<b&&b<c) return w(a,b,c-1)+w(a,b-1,c-1)+w(a,b-1,c);
    else return w(a-1,b,c)+w(a-1,b-1,c)+w(a-1,b,c-1)-w(a-1,b-1,c-1);
}
int main()
{
    ll a,b,c,ans;
    while(cin>>a>>b>>c)
    {
        if(a==-1&&b==-1&&c==-1) break;
        ans=w(a,b,c);
    cout<<"w("<<a<<","<<b<<","<<c<<")="<<ans<<endl;
    }
    return 0;
}

AC

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll h[40][40][40];
ll w(ll a,ll b,ll c)
{
    if(a<=20&&b<=20&&c<=20&&a>=0&&b>=0&&c>=0)if(h[a][b][c]) return h[a][b][c];
    if(a<=0||b<=0||c<=0) return 1;
    if(a>20||b>20||c>20) return w(20,20,20);
    if(a<b&&b<c) return h[a][b][c]=w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);
    return h[a][b][c]=w(a-1,b,c)+w(a-1,b-1,c)+w(a-1,b,c-1)-w(a-1,b-1,c-1);
}
int main()
{
    ll a,b,c,ans;
    while(cin>>a>>b>>c)
    {
        if(a==-1&&b==-1&&c==-1) break;
    //printf("w(%lld, %lld, %lld) = %lld\n",a,b,c,w(a,b,c));
    cout<<"w("<<a<<", "<<b<<", "<<c<<") = "<<w(a,b,c)<<endl;
    }
    return 0;
}

P5534 [XR-3] Equal Difference Sequence

meaning of the title
Say nothing about this

P1192 Step Problem

#include <bits/stdc++.h>
using namespace std;
#define ll long long
//There are two ways. The first is to simply find the law
//Second kind of Dp calculation
const int N=1e5+10;
const int mod=100003;
int d[N];
int main()
{
    int n,k;
    cin>>n>>k;
    d[0]=1;
    d[1]=1;
    for(int i=2;i<=n;i++)
    {
        for(int j=1;j<=k;j++)
        {
            if(i>=j)
            d[i]=(d[i-j]+d[i])%mod;
        }
    }
    //I don't understand at all when I obey
    cout<<(d[n]+mod)%mod<<endl;
    return 0;
}

P1025 [NOIP2001 Increase Group] Number Division

I like this topic

#include <iostream>

using namespace std;

int dp[205][10];
int main()
{
    int n,k;
    cin>>n>>k;
    for(int i=1; i<=n; i++) dp[i][1]=1;
    for(int i=1; i<=n; i++)
        for(int j=2; j<=k; j++)
        {
            //else if(i==j) dp[i][j]=1;
            if(i>=j) dp[i][j]=dp[i-1][j-1]+dp[i-j][j];
            else dp[i][j]=0;
        }
       // for(int i=1; i<=n; i++)
       //for(int j=1; j<=k; j++)
       //  cout<<dp[i][j]<<" ";
          cout<<dp[n][k]<<endl;
    return 0;
}
#include <iostream>

using namespace std;

//Searching is also a recursive process, learning other people's code purely

int n,k;
int dfs(int n,int p,int ii)//Represents the number to be allocated, the number of parts to be allocated, and the number to be selected at this time.
{
    if(p==1) return 1;//Exit recursion until p==1
    int sum=0;//Think carefully why it must be initialized within a function, because it's recursive here. With return, it's actually preserved
    for(int i=ii;i<=n/p;i++)//Pruning because enumeration to n would repeat
    {
        sum+=dfs(n-i,p-1,i);//This is easy to understand
    }
    return sum;
}
int main()
{
    cin>>n>>k;
    cout<<dfs(n,k,1)<<endl;
    return 0;
}

The starting point of P4994's final end

Not finished, updated Monday evening, 11.9

Posted by lunarul on Sun, 07 Nov 2021 08:27:37 -0800