训练补题-牛客训练赛7
# 牛客训练赛7补题记录
# J: Pointer Analysis
题解的做法让我想起了spfa
但是似乎不是很正确?
是否有这样一种情况:
- 存在命令的某一排列,使得,但的任意排列都无法使
题解也没有给出证明。
AC代码
#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define Android ios::sync_with_stdio(false), cin.tie(NULL)
using namespace std;
const int maxn = 200 + 10;
struct statement{
int type;
int left, left_ptr, right, right_ptr;
bool operator <(const statement & s)const{
return type < s.type;
}
}st[maxn];
statement analysis(const string & left, const string & right){
statement ret = {0, 0, 0, 0, 0};
if(left.size() == 1 && right.size() == 1){
ret.left = left[0] - 'A';
if(islower(right[0])){ // A = x
ret.type = 1;
ret.right = right[0] - 'a';
}else{ // A = B
ret.type = 2;
ret.right = right[0] - 'A';
}
}else{
ret.left = left[0] - 'A';
ret.right = right[0] - 'A';
if(left.size() == 1){ // A = B.f
ret.type = 3;
ret.right_ptr = right[2] - 'a';
}else{ // A.f = B
ret.type = 4;
ret.left_ptr = left[2] - 'a';
}
}
return ret;
}
int start[5], finish[5];
struct pointer{
bool points_to[28];
}ptr[27];
struct instance{
pointer ptr[28];
}ins[28];
void solve(){
memset(start, 0x3f, sizeof start);
int n; cin >> n;
string _buffer1, _buffer2;
for(int i=0; i<n; i++){
cin >> _buffer1 >> _buffer2 >> _buffer2;
st[i] = analysis(_buffer1, _buffer2);
}
sort(st, st + n);
for(int i=0; i<n; i++){
finish[st[i].type] = i;
if(start[st[i].type] == 0x3f3f3f3f) start[st[i].type] = i;
}
vector<bool> inqueue(28, false);
queue<int> work;
for(int i=start[1]; i<=finish[1]; i++){
int a = st[i].left, o = st[i].right;
if(!ptr[a].points_to[o]){
ptr[a].points_to[o] = true;
work.push(a);
}
}
while(!work.empty()){
while(!work.empty()){
int u = work.front(); work.pop();
inqueue[u] = false;
for(int i=start[2]; i<=finish[2]; i++){
if(st[i].right != u) continue;
int l = st[i].left;
bool updated = false;
for(int k=0; k<26; k++){
if(!ptr[l].points_to[k] && ptr[u].points_to[k]){
updated = true;
ptr[l].points_to[k] = true;
}
}
if(updated && !inqueue[l]) {
work.push(l);
inqueue[l] = true;
}
}
}
for(int i=start[4]; i<=finish[4]; i++){
int l = st[i].left, lptr = st[i].left_ptr, r = st[i].right;
for(int j=0; j<26; j++){
if(!ptr[l].points_to[j]) continue;
for(int k=0; k<26; k++){
ins[j].ptr[lptr].points_to[k] |= ptr[r].points_to[k];
}
}
}
for(int i=start[3]; i<=finish[3]; i++){
int l = st[i].left, r = st[i].right, rptr = st[i].right_ptr;
bool updated = false;
for(int j=0; j<26; j++){
if(!ptr[r].points_to[j]) continue;
for(int k=0; k<26; k++){
if(!ptr[l].points_to[k] && ins[j].ptr[rptr].points_to[k]){
updated = true;
ptr[l].points_to[k] = true;
}
}
}
if(updated && !inqueue[l]){
work.push(l);
inqueue[l] = true;
}
}
}
for(int i=0; i<26; i++){
cout << (char)('A' + i) << ": ";
for(int j=0; j<26; j++){
if(ptr[i].points_to[j]) cout << (char)('a' + j);
}
cout << '\n';
}
}
signed main(){
Android;
solve();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# A: Social Distancing
# C: A National Pandemic
# I: Valuable Forests
上次更新: 2021/02/24, 03:37:30