动态规划Code: #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; int MOD = 1000000007; class EllysPlaylists { public: int countPlaylists(vector <string> songs, int K); bool isSmooth(string a, string b); protected: private: }; bool EllysPlaylists::isSmooth(string a, string b) { for (int i=0; i<3; i++) { if(a[i] != b[b.length()-3 + i]) return false; } return true; } int EllysPlaylists::countPlaylists(vector<string> songs, int K) { int ans = 0; int n = songs.size(); vector<vector<int> > dp(n, vector<int>(K+1)); for (int i=0; i<n; i++) dp[i][1] = 1; for (int k=2; k<=K; k++) { for (int i=0; i<n; i++) { for (int j=0; j<n; j++) { if(isSmooth(songs[i],songs[j])) { dp[i][k]=(dp[i][k]+dp[j][k-1])%MOD; } } } } for (int i=0; i<n; i++) { ans = (ans + dp[i][K])%MOD; } return ans; }