Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #define _USE_MATH_DEFINES
- #include <iostream>
- #include <string>
- #include <vector>
- #include <cmath>
- #include <algorithm>
- #include <set>
- #include <map>
- #include <ctime>
- #include <cstring>
- #include <iomanip>
- #include <random>
- #include <unordered_set>
- #include <unordered_map>
- #include <deque>
- #include <queue>
- #include <bitset>
- #include <sstream>
- using namespace std;
- #define mp make_pair
- #define X first
- #define Y second
- #define all(x) x.begin(), x.end()
- #define all_(x) x.rbegin(), x.rend()
- typedef unsigned int ui;
- typedef long long ll;
- typedef long double ld;
- const int INF = 2e9 + 9;
- const int MAXN = 2e6 + 7;
- const ll MOD = 1e9 + 7;
- const ll ST = 157;
- const ll ST1 = 199;
- const ld EPS = 1e-9;
- const int BLOCK = 138;
- void solve();
- signed main(){
- srand('a' + 'l' + 'e' + 'x' + 'X' + '5' + '1' + '2');
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif // _DEBUG
- solve();
- }
- /*-------------------------------------------------------------------------------------------------------------*/
- struct br{
- int v[26];
- int last;
- br(){
- memset(v, 255, sizeof v);
- last = -1;
- }
- };
- vector<br> v;
- vector<int> ans;
- vector<pair<int, int>> parent;
- vector<pair<int, int>> suf;
- vector<string> vec;
- void update(string &s, int ind){
- int now = 0;
- for (int i = 0; i < s.size(); i++){
- if (v[now].v[s[i] - 'a'] == -1){
- v.emplace_back();
- v[now].v[s[i] - 'a'] = v.size() - 1;
- }
- now = v[now].v[s[i] - 'a'];
- int uk = v[now].last + 1;
- if (ans[ind] > suf[uk].first + 1 + i + 1){
- ans[ind] = suf[uk].first + 1 + i + 1;
- parent[ind] = mp(suf[uk].second, i + 1);
- }
- v[now].last = ind;
- }
- }
- void get(int l, int r, int n){
- v.clear();
- v.emplace_back();
- suf.clear();
- suf.assign(n, mp(0,0));
- parent.clear();
- parent.assign(n, mp(0, 0));
- ans.clear();
- ans.assign(n, 0);
- for (int i = 0; i < n; i++){
- suf[i] = mp(INF, i);
- ans[i] = INF;
- parent[i] = mp(0, -1);
- }
- ans[0] = 0;
- suf[0] = mp(0, 0);
- update(vec[l], 0);
- for (int i = 1; i < n; i++){
- ans[i] = ans[i - 1] + 1;
- parent[i] = mp(i - 1, -1);
- update(vec[(i + l) % n], i);
- suf[i] = mp(ans[i], i);
- for (int j = i - 1; j >= 0; j--){
- suf[j] = min(suf[j + 1], mp(ans[j], j));
- }
- }
- for (int j = n - 1; j >= 0; j--){
- if (ans[j]>ans[(j + 1) % n] + 1){
- ans[j] = ans[(j + 1) % n] + 1;
- parent[j] = mp((j + 1) % n, -2);
- }
- }
- vector<string> ans1;
- int uk = (r - l + n) % n;
- while (uk != 0){
- if (parent[uk].second > 0){
- for (int i = parent[uk].second - 1; i >= 0; i--){
- ans1.emplace_back();
- ans1.back().push_back(vec[(uk + l) % n][i]);
- }
- ans1.emplace_back("Alt");
- }
- else if (parent[uk].second == -1){
- ans1.emplace_back("down");
- }
- else{
- ans1.emplace_back("up");
- }
- uk = parent[uk].first;
- }
- cout << ans1.size() << "\n";
- for (int i = ans1.size() - 1; i >= 0; i--){
- cout << ans1[i] << "\n";
- }
- }
- void solve(){
- int n;
- cin >> n;
- for (int i = 0; i < n; i++){
- string s;
- cin >> s;
- vec.emplace_back(s);
- }
- int last = 0, k;
- cin >> k;
- for (int i = 0; i < k; i++){
- int a;
- cin >> a;
- get(last, a - 1, n);
- last = a - 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement