Alex_tz307

Grupe X - 6.6.5 / pag 174

Sep 22nd, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define INF 0x3f3f3f3f
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.     ios_base::sync_with_stdio(false);
  8.     cin.tie(nullptr);
  9.     cout.tie(nullptr);
  10.     int N;
  11.     cin >> N;
  12.     vector < pair < char , char > > a;
  13.     for(int i = 0; i < N; ++i) {
  14.         string s;
  15.         cin >> s;
  16.         a.emplace_back(s[0], s[s.size() - 1]);
  17.     }
  18.     vector < int > dp(N, INF);
  19.     for(int i = 0; i < N; ++i) {
  20.         for(int j = 0; j < i; ++j)
  21.             if(a[i].first == a[j].second && dp[i] > dp[j] + 1)
  22.                 dp[i] = dp[j] + 1;
  23.         if(dp[i] == INF)
  24.             dp[i] = 1;
  25.     }
  26.     int ans = 1;
  27.     for(int x : dp)
  28.         if(x != INF)
  29.             ans = max(ans, x);
  30.     cout << ans << '\n';
  31.     char last = '$';
  32.     vector < int > sol;
  33.     for(int i = ans; i > 0; --i) {
  34.         int poz = -1;
  35.         for(int j = 0; j < N; ++j)
  36.             if(dp[j] == i && (last == '$' || a[j].second == last)) {
  37.                 poz = j;
  38.                 last = a[j].first;
  39.                 break;
  40.             }
  41.         sol.emplace_back(poz + 1);
  42.     }
  43.     reverse(sol.begin(), sol.end());
  44.     for(int x : sol)
  45.         cout << x << '\n';
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment