AcWing 4958. 接龙数列
原题链接
中等
作者:
偷月亮的喵
,
2025-04-05 18:47:25
· 安徽
,
所有人可见
,
阅读 1
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
int f[N];
char a[N];
int b[N];
int g[10];
int main()
{
int n;
cin >> n;
char str[20];
for (int i = 1; i <= n; i++)
{
cin >> str;
a[i] = str[0] - '0';
b[i] = str[strlen(str) - 1] - '0';
}
for (int i = 1; i <= n; i++)
{
f[b[i]] = max(f[b[i]], f[a[i]] + 1);
}
int res = 0;
for (int i = 0; i <= 9; i++)
res = max(res, f[i]);
cout << n - res << endl;
return 0;
}