AcWing 5697. 金字塔消息方案
原题链接
简单
作者:
zsx123456
,
2024-10-20 20:56:46
,
所有人可见
,
阅读 3
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int n, idx, sum, mx;
vector<int> son[N];
string s[N];
map<string, int> mp;
void dfs(int l, int r)
{
if(l == r) return;
son[mp[s[l]]].push_back(l + 1);
int last = l + 1;
for(int i = last + 1; i < r; ++ i)
{
if(s[i] == s[last])
{
sum += 10;
dfs(last, i);
last = i;
}
}
}
void dfs_1(int u, int dep)
{
mx = max(mx, dep);
for(auto x : son[mp[s[u]]])
dfs_1(x, dep + 1);
}
int main()
{
int T;
cin >> T;
while(T --)
{
idx = 0, sum = 0, mx = 0;
cin >> n;
mp.clear();
for(int i = 1; i < N; ++ i) son[i].clear();
for(int i = 1; i <= n; ++ i)
{
cin >> s[i];
if(!mp.count(s[i])) mp[s[i]] = ++ idx;
}
s[0] = s[n];
int last = 0;
for(int i = 1; i <= n; ++ i)
{
if(s[i] == s[0])
{
sum += 10;
dfs(last, i);
last = i;
}
}
dfs_1(0, 0);
cout << (sum - mx * 10) * 2 << endl;
}
return 0;
}