Skip to content

Commit 0836861

Browse files
authored
update atcoder 175 f
1 parent d574405 commit 0836861

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

AtCoder/ABC175/F.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ tuple<string, string> calc(string a, string b)
8888
return mt(s, t);
8989
}
9090

91-
bool palin(string s)
91+
bool palin(string &s)
9292
{
9393
string t = s;
9494
reverse(all(t));
@@ -111,22 +111,24 @@ void solution()
111111
set<pair<int, pss>> pq;
112112
for (int i = 0; i < n; i++)
113113
{
114-
dist[mp(s[i], "")] = c[i];
115-
dist[mp("", s[i])] = c[i];
114+
if (!dist.count(mp(s[i], "")))
115+
dist[mp(s[i], "")] = INF;
116+
if (!dist.count(mp("", s[i])))
117+
dist[mp("", s[i])] = INF;
118+
dist[mp(s[i], "")] = min(dist[mp(s[i], "")], c[i]);
119+
dist[mp("", s[i])] = min(dist[mp("", s[i])], c[i]);
116120
pq.insert(mp(c[i], mp("", s[i])));
117121
pq.insert(mp(c[i], mp(s[i], "")));
118122
}
119123

120-
int ans = INF;
121124
while (!pq.empty())
122125
{
123126
int cur = pq.begin()->ff;
124127
string pre = pq.begin()->ss.ff;
125128
string suf = pq.begin()->ss.ss;
126129
pq.erase(pq.begin());
127-
128-
if (palin(pre + suf))
129-
ans = min(ans, cur);
130+
if (dist[mp(pre, suf)] != cur)
131+
continue;
130132

131133
//cout << "dist " << pre << " | " << suf << " = " << cur << endl;
132134
if (pre == "")
@@ -167,14 +169,18 @@ void solution()
167169
}
168170
}
169171

172+
int ans = INF;
170173
for (auto state : dist)
171174
{
172175
string a = state.ff.ff;
173176
string b = state.ff.ss;
174-
if (palin(a + b))
177+
bool ok = false;
178+
ok |= (a.size() == 0 && b.size() == 0);
179+
ok |= (a.size() == 0 && palin(b));
180+
ok |= (b.size() == 0 && palin(a));
181+
if (ok)
175182
ans = min(ans, state.ss);
176183
}
177-
178184
cout << (ans < INF ? ans : -1) << endl;
179185
}
180186

@@ -189,4 +195,3 @@ int32_t main()
189195
while (t--)
190196
solution();
191197
}
192-

0 commit comments

Comments
 (0)