AcWing 4958. 接龙数列
原题链接
中等
作者:
on1
,
2024-02-19 18:58:38
,
所有人可见
,
阅读 31
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
using ll = long long;
const int N = 1e5 + 100;
int n, l[N], r[N], f[N];
string str;
signed main()
{
cin >> n;
for (int i = 0; i < n; i ++ )
{
string str;
cin >> str;
l[i] = str[0] - '0';
r[i] = str[str.size() - 1] - '0';
}
for (int i = 0; i < n; i ++ ) f[r[i]] = max(f[r[i]], f[l[i]] + 1);
int cnt = -1;
for (int i = 0; i <= 9; i ++ ) cnt = max(cnt, f[i]);
cout << abs(n - cnt);
return 0;
}