Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- ifstream fin("file.in");
- const char newline('\n');
- const int NMAX(103);
- string text[NMAX];
- vector <unsigned> dp(NMAX, 1), poz(NMAX);
- int n;
- void Read()
- {
- while (fin >> text[++n])
- continue;
- n--;
- return;
- }
- string getSuf(string s)
- {
- string suf = "";
- unsigned l = s.size();
- suf += s[l - 2];
- suf += s[l - 1];
- return suf;
- }
- string getPref(string s)
- {
- string pref = "";
- pref += s[0];
- pref += s[1];
- return pref;
- }
- void display(int p)
- {
- if (!p)
- return;
- display(poz.at(p));
- cout << text[p] << newline;
- }
- int main()
- {
- Read();
- for (int j = 2; j <= n; j++)
- {
- for (int i = 1; i < j; i++)
- if (getPref(text[j]) == getSuf(text[i]) && dp[j] < dp[i] + 1)
- {
- dp.at(j) = dp.at(i) + 1;
- poz.at(j) = i;
- }
- }
- unsigned maxi = 0, pozmax;
- for (int i = 1; i <= n; i++)
- if (maxi < dp.at(i))
- {
- maxi = dp.at(i);
- pozmax = i;
- }
- display(pozmax);
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement