总结
这个题我当时想用动态规划DAG的思想,想了半天每想出来,其实就是用floyd的动态规划来跑最短路
老规矩
四个状态
第一个状态
计数器x
第二个状态
计数器y
第三个状态
num1
第四个状态
num2
所以dp[N][N][N][N],因为N=10,所以跑N2*N3=N^5 ----O(1e5),稳得一匹
N^2跑每一种计数器,因为计数器之间具有独立性,不存在转移关系
N^3跑floyd,跑完所有得情况就出来了
#include<bits/stdc++.h>
//typedef long long ll;
//#define ull unsigned long long
#define int long long
#define F first
#define S second
#define endl "\n"//<<flush
#define lowbit(x) (x&(-x))
#define ferma(a,b) pow(a,b-2)
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define memset(a,b) memset(a,b,sizeof(a));
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int MAXN=0x7fffffff;
//const long long INF = 0x3f3f3f3f3f3f3f3fLL;
void file()
{
#ifdef ONLINE_JUDGE
#else
freopen("cin.txt","r",stdin);
// freopen("cout.txt","w",stdout);
#endif
}
const int N=10;
int dp[N][N][N][N];
void init()
{
for(int i=0; i<10; i++)
for(int j=0; j<10; j++)
for(int k=0; k<10; k++)
for(int l=0; l<10; l++)
dp[i][j][k][l]=inf;
for(int x=0; x<10; x++)
{
for(int y=0; y<10; y++)
{
for(int k=0; k<10; k++)
{
dp[x][y][k][(k+x)%10]=1;
dp[x][y][k][(k+y)%10]=1;
}
}
}
}
void solve()
{
for(int x=0; x<10; x++)
{
for(int y=0; y<10; y++)
{
for(int k=0; k<10; k++)
{
for(int i=0; i<10; i++)
{
for(int j=0; j<10; j++)
{
dp[x][y][i][j]=min(dp[x][y][i][j],dp[x][y][i][k]+dp[x][y][k][j]);
}
}
}
}
}
}
signed main()
{
// IOS;
//file();
init();
solve();
string str;
cin>>str;
int len=str.size();
for(int i=0; i<10; i++)
{
for(int j=0; j<10; j++)
{
int ans=0,flag=1;
for(int k=0; k<len-1; k++)
{
ans+=dp[i][j][str[k]-'0'][str[k+1]-'0']-1;
if(dp[i][j][str[k]-'0'][str[k+1]-'0']==inf)
flag=0;
}
if(j)
cout<<" ";
cout<<(flag?ans:-1);
}
cout<<endl;
}
return 0;
}