# jerry_fuyi

Working out everything from the first principles.

## PAT甲级题分类汇编——线性

 题号 标题 分数 大意 时间 1054 The Dominant Color 20 寻找出现最多的数 200ms 1061 Dating 20 寻找字符串中相同字符 200ms 1071 Speech Patterns 25 寻找出现最多的单词 300ms 1077 Kuchiguse 20 字符串共同后缀 150ms 1082 Read Number in Chinese 25 中文读数 400ms 1084 Broken Keyboard 20 比较两序列的差异 200ms 1095 Cars on Campus 30 模拟车辆进出 300ms

1054：

 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 }

 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 }

1071：

 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 }

1082：

  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 }

1095：

 1 #include <iostream>
2 #include <iomanip>
3 #include <string>
4 #include <vector>
5 #include <map>
6 #include <algorithm>
7
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;
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     {
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 }

posted on 2019-08-10 17:52  jerry_fuyi  阅读(439)  评论(0编辑  收藏  举报