Nineteen hundred and sixty-five Bullock beginners

Keywords: PHP

It's very late at night, so I'll make a long story short.

1. Forms

 

Links: https://ac.nowcoder.com/acm/contest/917/C
Source: Niuke.com

Input Description:

The first row contains two integers n,Q

One string per line in the last n rows

The last Q line represents one operation per line:

One operation: 1 s

Second operation: 2

Output description:

For each two operations, answer.
Example 1

input

copy
4 4
play
the
pubg
game
1 game
1 pubg
2
2

output

copy
2
0

Explain



100% 1≤n,Q≤5×105100% 1≤n,Q≤5×105
 
For all input string lengths <=40


This link is good, let me enlighten when I do the problem.
https://blog.csdn.net/zhaohuaonline/article/details/21482687

Before this problem, I thought that the input of 44 was useless. As a result, I didn't program it directly. I planned to add it at the end of the program. As a result, I forgot that the problem of withdrawal cycle was time-consuming and labor-intensive, which affected the progress of solving the problem. Next time, first of all, 1. Write the input format first. 2. See the conditions that can be given as clearly as possible, and unite all forces that can be united.

Somehow flag ends with 0 each time causing an error.

Change cin to scanf to improve accuracy

Both of the above are compiler's own sb

It's worth noting that loops for map s can't be placed in loops and will time out.

Below I release the long-awaited map and iterator usage, it is really too convenient, the feeling of gun replacement is not the same!! Kaisen (/> =)/

map<string,int> word_count;
map<string,int>::iterator iter;
//Input string entry map
 for(int i=0;i<a1;i++)
    {
        cin>>word;
        ++word_count[word];
    }
//Yes map Traverse and manipulate the values
 for(iter = word_count.begin(); iter != word_count.end(); iter++)
    {
        res+=(iter->second-1);
        iter->second=1;
    }

There is an improvement in the way of thinking in this question:

The core of this topic is to ask, the number of character repetition, I have always felt that the number of times in the map statistics part, the number, began to worry about this, and then directly traverse each minus 1, is the number of repetitions.

But there are better ways. use

if(flag==1)
            {

                cin>>word;
//if The contents in parentheses are used. map If this word has not appeared before and no one has found it, then it must be.
//yes false,This means that there is repetition, and the number of repetitions is counted with a single variable only at this time.
//++It can reduce the computational burden.
                if(word_count[word]) res++;
                else word_count[word]=1;
                flag=0;
            }

Here is my solution:

#include<iostream>
#include<math.h>
#include<map>
#include <string>
#include<stdio.h>
#include <cstdlib>
using namespace std;
int main()
{
    map<string,int> word_count;
    map<string,int>::iterator iter;
    string word;
    int flag=-1;
    int res=0;
    int a1,a2;
    //scanf("%d %d",&a1,&a2);
    cin>>a1>>a2;
    for(int i=0;i<a1;i++)
    {
        cin>>word;
        ++word_count[word];
    }
    for(iter = word_count.begin(); iter != word_count.end(); iter++)
    {

        res+=(iter->second-1);
        iter->second=1;
    }

    for(int i=0;i<a2;i++)
    {
        //cout<<"flag:"<<flag<<endl;
        scanf("%d",&flag);
        //cout<<"flag1:"<<flag<<endl;
        //cin>>flag;

        if(flag==1)
            {

                cin>>word;
                if(word_count[word]) res++;
                else word_count[word]=1;
       
            }
        else
            {

                cout<<res<<endl;
                 res=0;
           

            }
    }
    return 0;
}

Real Big Man's Code, See What's Xinda Ya

//Membrane Big Man
#include<bits/stdc++.h>
using namespace std;
int n,q,res;
map<string,int> mp;
int main()
{
    scanf("%d %d",&n,&q);
    while(n--)
    {
        string str;
        cin>>str;
        if(mp[str]) res++;
        else    mp[str]=1;
    }
    while(q--)
    {
        int op;
        scanf("%d",&op);
        if(op==1)
        {
            string str;
            cin>>str;
            if(mp[str]) res++;
            else    mp[str]=1;
        }
        else
        {
            printf("%d\n",res);
            res=0;
        }
    }
    return 0;
}

2. Use POW cautiously

Especially, it's easy to have problems when we finish square work first.

int main()
{
    int n=100;
//If it is double Yes, the result could be 100.,If it is int The result is 99. Estimation is due to accuracy, but below
//Take care not to use pow,The better solution is to multiply directly. t*t,Anyway, it's only multiplied once, but it's stable and correct.
//The rate will be much higher.
    double t=sqrt(n);
    cout<<t<<endl;
    double k=pow(t,2);
    cout<<k<<endl;
    return 0;
}

3.

Pay special attention to an ideological problem

There are some problems that require the number of iterations. If you have a global variable count, remember to clear 0 before each iteration.

There are at least two questions to find the common factor this time. The direct traversal method is easy to overtime. Here we introduce a rolling phase division method.

Example 1. Find the maximum common factors of two positive numbers 8251 and 6105.
(analysis: rolling phase division residue zero result obtained)
Solution: 8251 = 6105 x 1 + 2146
Obviously, the greatest common factor of 8251 and 6105 is also the factor of 2146, and the common factor of 6105 and 2146 is also the factor of 8251, so the greatest common factor of 8251 and 6105 is also the greatest common factor of 6105 and 2146.
6105=2146×2+1813
2146=1813×1+333
1813=333×5+148
333=148×2+37
148=37×4+0
Then 37 is the greatest common factor of 8251 and 6105.
The above method of finding the greatest common factor is rolling division. Also called Euclid Algorithms, it is by Euclid stay 300 BC Left and right first put forward.

int gcd1(int a,int b)
{
  int  temp;
  if(a==0||b==0)
   return -1;
  if(a<b)
    { temp=a;a=b;b=temp;} //Find the maximum and minimum value. a Representation, small use b Express
   while(b!=0)            //Finding the Maximum Common Number by Circulation
    {
      temp=a%b;
      a=b;
      b=temp;
    }
  if(a==1)
    return -1;
  return a;              //Gcd
}

Posted by Incredinot on Fri, 14 Jun 2019 11:33:09 -0700