解题思路:利用一个数组记录最长的接龙数组的长度 详细在注释
注意事项:
参考代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
//找出最长接龙数组的长度 n-l就是答案
int dp[100];//以i结尾的接龙数组的长度
signed main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n; cin>>n;
string tmp;
for(int i=1;i<=n;i++){
cin>>tmp;
int a=tmp[0]-'0';
int b=tmp[tmp.size()-1]-'0';
dp[b]=max(dp[b],dp[a]+1);
}
int ans=0;
for(int i=0;i<=9;i++){
ans=max(ans,dp[i]);
}
ans=n-ans;
cout<<ans;
return 0;
}
0.0分
0 人评分
Biggest Number (C++代码)回溯法浏览:1614 |
2005年春浙江省计算机等级考试二级C 编程题(3) (C语言代码)浏览:390 |
C二级辅导-阶乘数列 (C语言代码)浏览:691 |
数组与指针的问题浏览:717 |
Tom数 (C语言代码)浏览:527 |
2006年春浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:344 |
永远的丰碑 (C语言代码)浏览:525 |
Pascal三角 (C语言代码)浏览:651 |
复数求和 (C语言代码)浏览:926 |
敲七 (C++代码)浏览:1056 |