Language: C++

#include<iostream>
#include<string>
#include<vector>
using namespace std;

bool check(int begin, int end, vector<vector<int> > &ind){
  for(int i = 0; i < (int)ind.size(); ++i){
    vector<int>::iterator it_b = lower_bound(ind[i].begin(),ind[i].end(),begin);
    vector<int>::iterator it_e = lower_bound(ind[i].begin(),ind[i].end(),end);
    bool next_b = false;
    if(it_b==ind[i].end())next_b=false;
    else{
      if(begin<=*it_b&&*it_b<=end)next_b=true;
    }

    bool next_e = false;
    while(it_e!=ind[i].begin()){
      --it_e;
      if(begin<=*it_e&&*it_e<=end){
        next_e = true;
        break;
      }
    }
    if(it_e==ind[i].begin()){
      if(begin<=*it_e&&*it_e<=end){
        next_e=true;
      }
    }
    if(!next_b||!next_e)return false;
  }
  return true;
}

int main()
{
  bool first = true;
  while(true){
    string text, dam;
    while(true){
      string tmp;
      if(!getline(cin,tmp))return 0;
      text+=tmp;
      if(tmp.length()==0)break;
    }
    string key;
    getline(cin,key);
    getline(cin,dam);

    if(first)first=false;
    else puts("");

    vector< vector<int> > ind(key.length());
    sort(key.begin(),key.end());
    for(int i = 0; i < (int)text.length();++i){
      string::iterator it = lower_bound(key.begin(),key.end(),text[i]);
      if(it!=key.end()){
        if( *it == text[i] ){
          ind[it-key.begin()].push_back(i);
        }
      }
    }

    // cout <<key<<endl;
    //     cout<<text<<endl;
    /*
    for(int i = 0; i < (int)ind.size(); ++i){
      for(int j = 0; j < (int)ind[i].size(); ++j){
        cout <<ind[i][j] << ' ';
      }
      cout << endl;
    }
    */
    
    int res_cnt = 0;
    int res_b = -1;
    int res_e = (int)text.length();
    for(int x = 0; x < (int)ind.size(); ++x){
      for(int i = 0; i < (int)ind[x].size(); ++i){
        int begin = ind[x][i];
        int left = begin;
        int right = text.length();
        int end;
        int cnt = 0;
        while(cnt++<25){
          end = (left+right)/2;
          if(check(begin,end,ind)){
            right = end;
          }else left = end;
        }
        for(int k = -2; k < 2; ++k){
          int tend=end+k;
          if(check(begin,tend,ind)){
            if(res_e-res_b>tend-begin){
              res_e = tend;
              res_b = begin;
              res_cnt = 1;
            }else if( res_e-res_b==tend-begin){
              ++res_cnt;
              if( begin<res_b){
                res_e = tend;
                res_b = begin;
              }
            }
            // cout << begin<<' ' <<tend<<' '<<tend-begin<<endl;
            break;
          }
        }
      }
    }

    // cout << res_b << ' ' << res_e << endl;
    if(res_cnt==0){
      printf("0\n");
    }else{
      int cnt = 0;
      printf("%d\n\n",res_cnt);
      for(int i = res_b; i <= res_e; ++i){
        putchar(text[i]);
        ++cnt;
        if(cnt==72){putchar('\n');cnt=0;}
      }
      if(cnt>0){
        puts("");
      }
    }



    
  }
  return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:53:5: error: 'sort' was not declared in this scope; did you mean 'short'?
   53 |     sort(key.begin(),key.end());
      |     ^~~~
      |     short