Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <stack>
- #include <set>
- #include <map>
- #define pii pair <int,int>
- #define vec vector
- using namespace std;
- using ll = long long;
- using ld = long double;
- using db = double;
- void cv(vector <int> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvl(vector <ll> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvv(vector <vector <int> > &v){
- for (auto x: v) cv(x);
- cout<<"\n";
- }
- void cvb(vector <bool> v){
- for (bool x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvs(vector <string> v){
- for (auto a: v){
- cout<<a<<"\n";
- }
- }
- void cvp(vector <pii> a){
- for (auto p: a){
- cout<<p.first<<' '<<p.second<<"\n";
- }
- cout<<"\n";
- }
- bool sh=0;
- string t;
- int n;
- vector <string> sv;
- struct in{
- int fr, ln, ids;
- };
- bool cmp(in a, in b){
- return a.fr < b.fr || a.fr == b.fr && a.ln > b.ln;
- }
- void slv(){
- cin>>t>>n;
- sv.resize(n);
- for (string &i: sv) cin>>i;
- vector <in> al;
- if (sh){
- cout<<"A\n";
- }
- in x;
- int id=-1;
- for (string s: sv){
- id++;
- if (sh){
- //cout<<"id s = "<<id<<' '<<s<<"\n";
- }
- for (int i = 0; i <= t.size() - s.size(); ++i){
- if (t.substr(i, s.size()) == s){
- if (sh){
- //cout<<"found\n";
- }
- x = {i, s.size(), id};
- al.push_back(x);
- }
- }
- }
- /*
- abacabaca
- 3
- aba
- bac
- aca
- */
- sort(al.begin(), al.end(), cmp);
- if (al.empty()){
- cout<<-1<<"\n";
- return;
- }
- int from=0,to=0;
- vector <pii> ans;//опследовательность жадных ходов
- id=0;//ЖАДНОСТЬ
- bool can=1;
- pii mv; // move
- if (sh){
- cout<<"B\n";
- cout<<"al.size() = "<<al.size()<<"\n";
- cout<<"al\n";
- for (auto p: al){
- cout<<p.fr<<' '<<p.ln<<' '<<p.ids<<"\n";
- }
- }
- while (from < t.size() && to < t.size()){
- vector <pii> now; //{длина за зоной изведанного, id из al}
- //id = from;
- if (sh){
- cout<<"from to = "<<from<<' '<<to<<"\n";
- }
- while (id < al.size() && al[id].fr <= to){
- now.push_back({al[id].fr + al[id].ln - 1 - (to - 1), id});
- if (sh){
- cout<<"id = "<<id<<"\n";
- }
- id++;
- }
- sort(now.begin(), now.end());
- reverse(now.begin(), now.end());
- if (sh){
- //cout<<"C\n";
- cout<<"now\n";
- cvp(now);
- if (sh){
- cout<<"ans\n";
- cvp(ans);
- }
- }
- //берем первый
- if (now.empty() || now[0].first <= 0){//значит не переплюнули границу неизведанного
- can=0;
- break;
- }
- in gd = al[now[0].second];
- to = gd.fr + gd.ln;
- mv = {gd.ids, gd.fr};
- ans.push_back(mv);
- from++;
- }
- if (sh){
- cout<<"id = "<<id<<"\n";
- cout<<"NIGGER ASS FUCK\n";
- }
- if (!can){
- cout<<-1<<"\n";
- return;
- }
- cout<<ans.size()<<"\n";
- for (auto p: ans){
- cout<<(p.first+1)<<' '<<(p.second+1)<<"\n";
- }
- }
- /*
- На 2м тесте все вылетает -- проверь, а на 1м WA
- */
- /*
- abacabaca
- 3
- aba
- bac
- aca
- */
- int main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- int q=1;
- if (!sh) cin>>q;
- for (int go=0;go<q;++go){
- slv();
- }
- }
Add Comment
Please, Sign In to add comment