PAT Class A compilation of questions classified - Linear

Linear type, refers to a linear time complexity can complete title. In 1051 to 1100, there are 7:

Question number title fraction main idea time
1054 The Dominant Color 20 Looking up to the emergence of a number 200ms
1061 Dating 20 Looking for the same character string 200ms
1071 Speech Patterns 25 Looking up to the emergence of the word 300ms
1077 Kuchiguse 20 Common suffix string 150ms
1082 Read Number in Chinese 25 Chinese reading 400ms
1084 Broken Keyboard 20 Comparison between two sequences 200ms
1095 Cars on Campus 30 Analog vehicle access 300ms

See generally linear title score is not high, generally only the title of the event will be a simulation 30 points, but it is not difficult.

This problem will generally look to do (the largest sub-columns and except), general difficulty in the details of the deal (All questions are PAT) and the time constant.

About details of the deal, the low score title, already a simple, easy to remember to do when some of the details, before the first submission on the deal.

Time limits, time limits other problems are generally 400ms, but the class of linear time tighter more, this time we should be careful of the constant.

 

Read a little seven questions, we decided to do 1054 , 1071 , 1082 and 1095 a total of four.

 

1054:

Placed under linear classification, it is necessary to do with the linear method. Using  std :: map  to make it look very simple, to challenge yourself, to put it another method.

The general idea is that each dimension (color) to create a hash table, a pointer to the next dimension of the hash table is stored, the last dimension data store.

code show as below:

 1 #include <iostream>
 2 #include <vector>
 3 #include <memory>
 4 
 5 template <typename T>
 6 using Pointer_vector = std::shared_ptr<std::vector<T>>;
 7 
 8 int main()
 9 {
10     int m, n, total;
11     std::cin >> m >> n;
12     total = m * n;
13     using Blue = int;
14     using Green = Pointer_vector<Blue>;
15     using Red = Pointer_vector<Green>;
16     std::vector<Red> data(256);
17     for (int cnt = 0; cnt != total; ++cnt)
18     {
19         int color;
20         std::cin >> color;
21         int red = color >> 16;
22         int green = (color >> 8) % 256;
23         int blue = color % 256;
24         auto& red_data = data[red];
25         if (!red_data)
26             red_data = Red(new std::vector<Green>(256));
27         auto& green_data = (*red_data)[green];
28         if (!green_data)
29             green_data = Green(new std::vector<Blue>(256));
30         auto& blue_data = (*green_data)[blue];
31         ++blue_data;
32     }
33     try
34     {
35         for (int r = 0; r != 256; ++r)
36             if (data[r])
37                 for (int g = 0; g != 256; ++g)
38                     if ((*data[r])[g])
39                         for (int b = 0; b != 256; ++b)
40                             if ((*(*data[r])[g])[b] > total / 2)
41                                 throw (r << 16) + (g << 8) + b;
42     }
43     catch (int res)
44     {
45         std::cout << res;
46     }
47 }

For multiple cycles to  break  the algorithms I usually do with abnormal flow control. This is a controversial practice, so I think the output and  return  replace the original  throw  statement, did not think the overtime! Plus optimized useless.

Not to say that  the try  Block will lead to performance degradation it? Why add after exception handling performance but improve?

Can not figure out. I use  std :: map  implements an algorithm:

 1 #include <iostream>
 2 #include <map>
 3 
 4 #pragma GCC optimize(O3)
 5 
 6 int main()
 7 {
 8     int m, n, total;
 9     std::cin >> m >> n;
10     total = m * n;
11     std::map<int, int> map;
12     for (int cnt = 0; cnt != total; ++cnt)
13     {
14         int color;
15         std::cin >> color;
16         ++map[color];
17     }
18     for (const auto& pair : map)
19         if (pair.second > total / 2)
20         {
21             std::cout << pair.first;
22             return 0;
23         }
24     return 0;
25 }

Of course, there is no beginning  #pragma  that line, but overtime, and later added only AC.

 

This question tells us that the time-linear problem really high. Perhaps with  scanf  instead of  std :: cin  make the original AC overtime, and I have not tried.

 

1071:

Linear processing of the string, std :: the Map  Use of this question so much difficulty. code show as below:

 1 #include <iostream>
 2 #include <string>
 3 #include <map>
 4 #include <cctype>
 5 
 6 int main()
 7 {
 8     std::map<std::string, int> words;
 9     std::string string;
10     while (1)
11     {
12         char c = std::cin.get();
13         if (std::isalnum(c))
14             string.push_back(std::tolower(c));
15         else
16             if (!string.empty())
17             {
18                 ++words[string];
19                 string.clear();
20             }
21         if (c == '\n')
22             break;
23     }
24     auto max = words.cbegin();
25     for (auto iter = words.cbegin(); iter != words.cend(); ++iter)
26         if (iter->second > max->second)
27             max = iter;
28     std::cout << max->first << ' ' << max->second << std::endl;
29 }

Bars are worth a bumper This question is not linear.

First, the input is O (n), the output is O (1), are within the linear range.

There are a kind of a word hypothesis of  std :: map  operations is O (a · loga); the minimum length of a word connected together species is O (a · loga), n must be greater than this length, so this question is linear of.

 

1082:

Oh, this question is in the mathematics test. But I did not put it in the kind of math, because this is the Primary Mathematics.

I find it difficult to start, but as long as less than 10,000 digital read how to figure out, This question is almost the same. code show as below:

  1 #include <iostream>
  2 #include <string>
  3 #include <vector>
  4 
  5 std::vector<std::string> data;
  6 
  7 const char pinyin[10][5] =
  8 {
  9     "ling",
 10     "yi",
 11     "er",
 12     "san",
 13     "si",
 14     "wu",
 15     "liu",
 16     "qi",
 17     "ba",
 18     "jiu"
 19 };
 20 
 21 void chinese(int num)
 22 {
 23     int qian = num / 1000;
 24     num %= 1000;
 25     int bai = num / 100;
 26     num %= 100;
 27     int shi = num / 10;
 28     int ge = num % 10;
 29     int digit = 0;
 30     if (qian)
 31     {
 32         digit = 3;
 33         data.push_back(pinyin[qian]);
 34         data.push_back("Qian");
 35     }
 36     if (bai)
 37     {
 38         digit = 2;
 39         data.push_back(pinyin[bai]);
 40         data.push_back("Bai");
 41     }
 42     if (shi)
 43     {
 44         if (digit > 2)
 45             data.push_back(pinyin[0]);
 46         digit = 1;
 47         data.push_back(pinyin[shi]);
 48         data.push_back("Shi");
 49     }
 50     if (ge)
 51     {
 52         if (digit > 1)
 53             data.push_back(pinyin[0]);
 54         data.push_back(pinyin[ge]);
 55     }
 56 }
 57 
 58 int main()
 59 {
 60     int num;
 61     std::cin >> num;
 62     if (num == 0)
 63     {
 64         std::cout << pinyin[0];
 65         return 0;
 66     }
 67     if (num < 0)
 68     {
 69         num = -num;
 70         data.push_back("Fu");
 71     }
 72     int yi = num / 100000000;
 73     int wan = num % 100000000 / 10000;
 74     int ge = num % 10000;
 75     int seg = 0;
 76     if (yi)
 77     {
 78         seg = 2;
 79         data.push_back(pinyin[yi]);
 80         data.push_back("Yi");
 81     }
 82     if (wan)
 83     {
 84         if (wan < 1000 && seg > 1)
 85             data.push_back(pinyin[0]);
 86         seg = 1;
 87         chinese(wan);
 88         data.push_back("Wan");
 89     }
 90     if (ge)
 91     {
 92         if (ge < 1000 && seg > 0)
 93             data.push_back(pinyin[0]);
 94         chinese(ge);
 95     }
 96     int end = data.size() - 1;
 97     for (int i = 0; i != end; ++i)
 98         std::cout << data[i] << ' ';
 99     std::cout << data[end];
100 }

This is my first time with the alphabet to name variables. I do not want this, but Who Chinese background of this question so obvious it?

 

1095:

This question requires process simulation vehicles entering and leaving the campus, such a process simulation title, I call it simulation class title.

A general approach is to simulate the class title as a variable cycle time, read, read this question but to use it another way, is an object-variable cycle, the object here is to record vehicles entering and leaving.

Title logic is more complex, it can be divided into three steps:

The first step reads all records, chronological order, then by matching record type;

The second step, the reading time of the query, and vehicles entering and leaving the simulation process, which is the core problem of simulation class (but not accounted for in this title in proportion);

The third step is to find the longest stop time, and outputs a corresponding license plate number (old driver to drive!).

After teasing the pile logic directly on the bar code:

 1 #include <iostream>
 2 #include <iomanip>
 3 #include <string>
 4 #include <vector>
 5 #include <map>
 6 #include <algorithm>
 7 
 8 int read_time()
 9 {
10     int res, temp;
11     std::cin >> temp;
12     res = temp * 3600;
13     std::cin.get();
14     std::cin >> temp;
15     res += temp * 60;
16     std::cin.get();
17     std::cin >> temp;
18     res += temp;
19     return res;
20 }
21 
22 void print_time(int time)
23 {
24     std::cout << std::setfill('0');
25     std::cout << std::setw(2) << time / 3600 << ':';
26     time %= 3600;
27     std::cout << std::setw(2) << time / 60 << ':';
28     std::cout << std::setw(2) << time % 60;
29 }
30 
31 enum class Status
32 {
33     in, out
34 };
35 
36 struct Record
37 {
38     std::string plate;
39     int time;
40     Status status;
41     bool paired = false;
42     int parking;
43 };
44 
45 int main()
46 {
47     int n, k;
48     std::cin >> n >> k;
49     std::vector<Record> records(n);
50     for (auto& r : records)
51     {
52         std::cin >> r.plate;
53         r.time = read_time();
54         std::string str;
55         std::cin >> str;
56         r.status = str == "in" ? Status::in : Status::out;
57     }
58     std::sort(records.begin(), records.end(), [](const Record& _lhs, const Record& _rhs) {
59         return _lhs.time < _rhs.time;
60     });
61     for (auto rec = records.begin(); rec != records.end(); ++rec)
62         if (rec->status == Status::in)
63             for (auto iter = rec + 1; iter != records.end(); ++iter)
64                 if (iter->plate == rec->plate && iter->status == Status::in)
65                     break;
66                 else if (iter->plate == rec->plate && iter->status == Status::out)
67                 {
68                     rec->paired = iter->paired = true;
69                     rec->parking = iter->time - rec->time;
70                     break;
71                 }
72 
73     auto iter = records.begin();
74     int count = 0;
75     for (int cnt = 0; cnt != k; ++cnt)
76     {
77         int time = read_time();
78         for (; iter != records.end() && iter->time <= time; ++iter)
79             if (iter->paired && iter->status == Status::in)
80                 ++count;
81             else if (iter->paired && iter->status == Status::out)
82                 --count;
83         std::cout << count << std::endl;
84     }
85 
86     std::map<std::string, int> parking;
87     for (const auto& rec : records)
88         if (rec.paired && rec.status == Status::in)
89             parking[rec.plate] += rec.parking;
90     int longest = 0;
91     for (const auto& car : parking)
92         if (car.second > longest)
93             longest = car.second;
94     for (const auto& car : parking)
95         if (car.second == longest)
96             std::cout << car.first << ' ';
97     print_time(longest);
98 }

This question is on the linear class, because the pairing and simulation algorithms are linear (in fact, because I see it is Simulators put it points to the linear class).

 

In short, linear type item difficulty is not high, but a lot of the pit, only a variety of boundary data, as well as the time constant of the card.

 

Guess you like

Origin www.cnblogs.com/jerry-fuyi/p/11258506.html