PAT 2019 autumn

  Examination okay. But a little bit of regret, but did not expect to go straight to the first question and sort dfs, feeling the difficulty of the subject seems to be the same as backwards. Ha ha ha ha.

  The first question really do collapse mind, I feel this question is that you can do, but in fact almost always what.

  The second question, the third question, to my mind stabilize, otherwise the exam may be an avalanche. These two questions are submitted by a road, a direct wholly-red.

  Three questions then brought confidence to get the fourth question, it seems submitted twice.

  Then again start Sike first question, the key was the first question you think you should not use dfs, it has been modified in detail. ╮ (╯ ▽ ╰) ╭, or timeout.

  PAT is not difficult to feel, but under the influence of the new environment, the timing, queuing submitted and other factors, there is considerable test of psychological qualities (at least for many people), the first question may directly lead to the entire field examination steady no longer.

  Anyway strength is strong enough to reduce the impact of these factors.

Here is the test time code.

7-1 Forever

Felt the first question, 3000ms, direct violence calculated. (A point last time out)

 1 #include <cstdio>
 2 #include <vector>
 3 #include <cmath>
 4 #include <iostream>
 5 #include <map>
 6 #include <algorithm>
 7 using namespace std;
 8 int N, K, m;
 9 typedef struct NODE{
10     int a, n;
11     NODE(int tmpN, int tmpA):a(tmpA),n(tmpN){}
12 }node;
13 int gcd(int a, int b){
14     if(b == 0)    return a;
15     return gcd(b, a%b);
16 }
17 bool isPrimeB2(int n){
18     if(n <= 2)
19         return false;
20     int sqr = (int)sqrt(1.0*n);
21     for(int i = 2; i <= sqr; ++ i)
22         if(n % i == 0)
23             return false;
24     return true;
25 }
26 int getSumOfDigit(int n){
27     int tmpSum = 0;
28     while(n > 0){
29         tmpSum += n %10;
30         n /= 10;
31     }
32     return tmpSum;
33 }
34 bool cmpSort(node a, node b){
35     if(a.n != b.n)
36         return a.n < b.n;
37     else if(a.a != b.a)
38         return a.a < b.a;
39 }
40 vector<node> dstVec[100];
41 int fac[11]={0,1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};
42 int main(){
43     cin >> N;
44 
45     int tmpN;
46     for(int i = 1; i <= N; ++ i){
47         scanf("%d %d", &K, &m);
48         int cnt = 0;
49         printf("Case %d\n", i);
50         for(int j = fac[K]; j < fac[K+1]; j++){
51             if(getSumOfDigit(j) == m){
52                 tmpN = getSumOfDigit(j+1);
53                 int tmpNum = gcd(m, tmpN);
54                 if(isPrimeB2(tmpNum)){
55                     cnt ++;
56                     dstVec[tmpN].push_back(NODE(tmpN, j));
57                 }
58             }
59         }
60         
61         if(cnt == 0)
62             printf("No Solution\n");
63         else{
64             for(int k = 1; k < 100; ++ k){
65                 sort(dstVec[k].begin(), dstVec[k].end(), cmpSort);
66             for(auto it = dstVec[k].begin(); it != dstVec[k].end(); ++ it)
67                 printf("%d %d\n", it->n, it->a);
68                 dstVec[k].clear();
69             }
70         }
71     }
72     return 0;
73 }
View Code

7-2 Merging Linked Lists

This question is nothing to say, Grade Exams, there are similar.

 1 #include <cstdio>
 2 #include <vector>
 3 #include <cmath>
 4 #include <iostream>
 5 #include <map>
 6 using namespace std;
 7 int L1, L2, N;
 8 typedef struct NODE{
 9     int val, next, addr;
10     NODE(){}
11     NODE(int a, int v, int n):addr(a),val(v),next(n){}
12 }node;
13 vector<node> inputNodeVec(1000010);
14 vector<node> node1Vec, node2Vec, dstVec;
15 void adjust(vector<node> &lVec1, vector<node> &lVec2){
16     int len1 = 0, len2 = lVec2.size()-1, cnt = 0;
17     while(len1 < lVec1.size() && len2 >= 0){
18         if(cnt < 2){
19             dstVec.push_back(lVec1[len1++]);
20             cnt ++;
21         }
22         else{
23             dstVec.push_back(lVec2[len2--]);
24             cnt = 0;
25         }
26     }
27     while(len1 < lVec1.size())
28         dstVec.push_back(lVec1[len1++]);
29     while(len2 >= 0)
30         dstVec.push_back(lVec2[len2--]);
31 }
32 int main()
33 {
34     cin >> L1 >> L2 >> N;
35     int tmpAddr, tmpNext, tmpVal;
36     while(N--){
37         scanf("% d% d% d " , & tmpAddr, & tmpVal, & tmpNext);
 38          inputNodeVec [tmpAddr] .next = tmpNext;
 39          inputNodeVec [tmpAddr] .val = tmpVal;
 40          inputNodeVec [tmpAddr] .addr = tmpAddr;
 41      }
 42      int cnt1 = 0 , cnt2 = 0 ;
 43      tmpAddr = L1;
 44      while (tmpAddr! = - 1 ) {
 45          node1Vec.push_back (inputNodeVec [tmpAddr]);
 46          tmpAddr = inputNodeVec [tmpAddr] .next;
 47      }
48     tmpAddr = L2;
49     while(tmpAddr != -1){
50         node2Vec.push_back(inputNodeVec[tmpAddr]);
51         tmpAddr = inputNodeVec[tmpAddr].next;
52     }
53     if(node1Vec.size() > node2Vec.size()){
54         adjust(node1Vec, node2Vec);
55     }
56     else{
57         adjust(node2Vec, node1Vec);
58     }
59     int tmpLen = dstVec.size();
60     for(int i = 0; i < tmpLen; ++ i){
61         if(i == tmpLen-1)
62             printf("%05d %d -1\n", dstVec[i].addr, dstVec[i].val);
63         else
64             printf("%05d %d %05d\n", dstVec[i].addr, dstVec[i].val, dstVec[i+1].addr);
65     }
66     return 0;
67 }

7-3 Postfix Expression

  This question is the basis for the title.

 1 #include <cstdio>
 2 #include <vector>
 3 #include <cmath>
 4 #include <iostream>
 5 #include <map>
 6 #include <string>
 7 #include <stack>
 8 using namespace std;
 9 int L1, L2, N;
10 typedef struct NODE{
11     string symbolStr;
12     int lChild, rChild;
13     NODE(){}
14     NODE(string str, int v, int n):symbolStr(str),lChild(v),rChild(n){}
15 }node;
16 vector<node> synTreeVec;
17 vector<bool> visitFlagVec;
18 string preStr;
19 string preOrder(int u){
20     if(u == -1)
21         return "";
22     string tmpStr1 = preOrder(synTreeVec[u].lChild);
23     string tmpStr2 = preOrder(synTreeVec[u].rChild);
24     if(tmpStr1.empty())
25         return    "( " + SynTreeVec [u] + .symbolStr tmpStr2 + " ) " ?
 26      else clauses 
27          return     " ( " + + tmpStr1 tmpStr2 + synTreeVec [u] .symbolStr + " ) " ?
 28  }
 29  bool isSyn ( char tmpC) {
 30      the if (tmpC == ' + ' || tmpC == ' - ' || tmpC == ' * ' || tmpC == ' / ' || tmpC == ' % ')
31         return true;
32     return false;
33 }
34 
35 int main(){
36     int N;
37     cin >> N;
38     synTreeVec.resize(N+1);
39     visitFlagVec.resize(N+1, false);
40     string tmpStr;
41     int tmpL, tmpR;
42     for(int i = 1; i <= N; ++ i){
43         cin >> synTreeVec[i].symbolStr >> tmpL >> tmpR;
44         synTreeVec[i].lChild = tmpL;
45         synTreeVec[i].rChild = tmpR;
46         if(tmpL > 0)
47             visitFlagVec[tmpL] = true;
48         if(tmpR > 0)
49             visitFlagVec[tmpR] = true;
50     }
51     int tmpRoot = 1;
52     for(int i = 1; i <= N; ++ i){
53         if(!visitFlagVec[i]){
54             tmpRoot = i;
55             break;
56         }
57     }
58     preStr = preOrder(tmpRoot);
59     cout << preStr;
60     return 0;
61 }

7-4 Dijkstra Sequence

  The main question is the final judge. The same point source as the shortest path to the same level point, the position of a point can be interchanged.

 1 #include <cstdio>
 2 #include <vector>
 3 #include <cmath>
 4 #include <iostream>
 5 #include <map>
 6 #include <string>
 7 #include <stack>
 8 using namespace std;
 9 int Nv, Ne;
10 const int INF = 0x7f7f7f7f;
11 typedef struct NODE{
12     int val, next;
13     NODE(){}
14     NODE(int n, int v):val(v),next(n){}
15 }node;
16 vector<node> routeVec[1010];
17 vector<int> numSequen,disVec;
18 vector<int> sameLevelNode[1010];
19 map<int, int> disMap, visFlagMap;
20 bool dijkstra(int u){
21     fill(disVec.begin(), disVec.end(), INF);
22     vector<bool> dijVisFlagVec(Nv+1,false);
23     disVec[u] = 0;
24     for(int i = 0; i < Nv; ++ i){
25         int tmpMin = INF, v = -1;
26         for(int j = 1; j <= Nv; j ++ ){
27             if(!dijVisFlagVec[j] && disVec[j] < tmpMin){
28                 v = j;
29                 tmpMin = disVec[j];
30             }
31         }
32         if(v == -1)
33             break;
34         dijVisFlagVec[v] = true;
35         for(auto it = routeVec[v].begin(); it != routeVec[v].end(); ++ it){
36             int tmpV = it->next, tmpW = it->val;
37             if(!dijVisFlagVec[tmpV] && disVec[tmpV] > disVec[v] + tmpW)
38                 disVec[tmpV] = disVec[v] + tmpW;
39         }
40     }
41     return true;
42 }
43 int main()
44 {
45     cin >> Nv >> Ne;
46     int tmpSt, tmpEnd, tmpDis;
47     while(Ne--){
48         scanf("%d %d %d", &tmpSt, &tmpEnd, &tmpDis);
49         routeVec[tmpSt].push_back(NODE(tmpEnd, tmpDis));
50         routeVec[tmpEnd].push_back(NODE(tmpSt, tmpDis));
51     }
52     int K;
53     disVec.resize(Nv+1, INF);
54     cin >> K;
55     while(K--){
56         disMap.clear();
57         visFlagMap.clear();
58         numSequen.resize(Nv, 0);
59         for(int i = 0; i < Nv; ++ i)
60             scanf("%d", &numSequen[i]);
61         bool tmpFlag = true;
62         dijkstra(numSequen[0]);
63         for(auto i = 1; i <= Nv; ++ i)
64             disMap[disVec[i]] ++;
65         int tmpIndex = 0, tmpMaxIndex = 0, tmpIndexDij = 1;
66         for(auto it = disMap.begin(); it != disMap.end(); ++ it){
67             tmpMaxIndex += (it->second);
68             tmpDis = it->first;
69             int p = 1;
70             while(p <= Nv && tmpIndexDij <= tmpMaxIndex){
71                 if(disVec[p] == tmpDis){
72                     visFlagMap[p] = 1;
73                     tmpIndexDij ++;
74                 }
75                 p++;
76 
77             }
78             while(tmpIndex < tmpMaxIndex){
79                 int tmpNum = numSequen[tmpIndex++];
80                 if(visFlagMap[tmpNum] != 1)
81                     tmpFlag = false;
82                 else
83                     visFlagMap[tmpNum] = 0;
84             }
85         }
86         if(tmpFlag)
87             printf("Yes\n");
88         else
89             printf("No\n");
90     }
91     return 0;
92 }

 

Guess you like

Origin www.cnblogs.com/codewars/p/11497068.html