Problem:
Problem H:
Co-occurrence Search
A huge amount of information is being heaped on WWW. Albeit it is not well-organized, users can browse WWW as an unbounded source of up-to-date information, instead of consulting established but a little out-of-date encyclopedia. However, you can further exploit WWW by learning more about keyword search algorithms.
For example, if you want to get information on recent comparison between Windows and UNIX, you may expect to get relevant description out of a big bunch of Web texts, by extracting texts that contain both keywords "Windows" and "UNIX" close together.
Here we have a simplified version of this co-occurrence keyword search problem, where the text and keywords are replaced by a string and key characters, respectively. A character string S of length
n
(1 ≤
n
≤ 1,000,000) and a set
K
of
k
distinct key characters
a
1
, ...,
a
k
(1 ≤
k
≤ 50) are given. Find every shortest substring of
S
that contains all of the key characters
a
1
, ...,
a
k
.
Input
The input is a text file which contains only printable characters (ASCII codes 21 to 7E in hexadecimal) and newlines. No white space such as space or tab appears in the input.
The text is a sequence of the shortest string search problems described above. Each problem consists of character string
S
i
and key character set
K
i
(
i
= 1, 2, ...,
p
). Every
S
i
and
K
i
is followed by an empty line. However, any single newline between successive lines in a string should be ignored; that is, newlines are not part of the string. For various technical reasons, every line consists of at most 72 characters. Each key character set is given in a single line. The input is terminated by consecutive empty lines;
p
is not given explicitly.
Output
All of
p
problems should be solved and their answers should be output in order. However, it is not requested to print all of the shortest substrings if more than one substring is found in a problem, since found substrings may be too much to check them all. Only the number of the substrings together with their representative is requested instead. That is, for each problem
i
, the number of the shortest substrings should be output followed by the first (or the leftmost) shortest substring
s
i
1
, obeying the following format:
the number of the shortest substrings for the i-th problem
    empty line
    the first line of s
i1
the second line of s
i1
...
    the last line of s
i1
empty line for the substring termination
where each line of the shortest substring
s
i
1
except for the last line should consist of exactly 72 characters and the last line (or the single line if the substring is shorter than or equal to 72 characters, of course) should not exceed 72 characters.
If there is no such substring for a problem, the output will be a 0 followed by an empty line; no more successive empty line should be output because there is no substring to be terminated.
Sample Input
Thefirstexampleistrivial.

mfv

AhugeamountofinformationisbeingheapedonWWW.Alb
...(truncated)

Submitted solutions:

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


string buf,line,pattern;
vector<int> LOC[128];


bool check(int start,int end){

	bool FLG;

	int left,right,m,tmp_start,tmp_end;

	for(int i = 0; i < pattern.length(); i++){
		int ch = pattern[i];

		left = 0,right = LOC[ch].size()-1,m = (left+right)/2;
		tmp_start = BIG_NUM;

		while(left <= right){
			if(LOC[ch][m] >= start){
				tmp_start = m;
				right = m-1;
			}else{
				left = m+1;
			}
			m = (left+right)/2;
		}
		if(tmp_start == BIG_NUM)return false;

		left = tmp_start,right = LOC[ch].size()-1,m = (left+right)/2;
		tmp_end = BIG_NUM;

		while(left <= right){
			if(LOC[ch][m] <= end){
				tmp_end = m;
				left = m+1;
			}else{
				right = m-1;
			}
			m = (left+right)/2;
		}
		if(tmp_end == BIG_NUM)return false;
	}
	return true;
}

void func(){

	for(int i = 0; i < 128; i++)LOC[i].clear();

	line += buf;
	while(true){
		getline(cin,buf);
		if(buf == "")break;
		line += buf;
	}
	getline(cin,pattern);
	getline(cin,buf);

	for(int i = 0; i < line.length(); i++){
		LOC[line[i]].push_back(i);
	}

	for(int i = 0; i < pattern.length(); i++){
		if(LOC[pattern[i]].size() == 0){
			printf("0\n\n");
			return;
		}
	}

	bool FLG;
	int left,right,m,minimum = BIG_NUM,ans_count = 1,tmp_end;
	int ans_start,ans_end;

	for(int start = 0; start < line.length(); start++){

		left = start,right = line.length()-1,m = (left+right)/2;
		tmp_end = BIG_NUM;

		while(left <= right){
			if(check(start,m)){
				tmp_end = m;
				right = m-1;
			}else{
				left = m+1;
			}
			m = (left+right)/2;
		}
		if(tmp_end == BIG_NUM)continue;

		if(tmp_end-start+1 < minimum){

			minimum = tmp_end-start+1;
			ans_count = 1;
			ans_start = start;
			ans_end = tmp_end;

		}else if(tmp_end-start+1 == minimum){
			ans_count++;
		}
	}

	printf("%d\n\n",ans_count);
	for(int i = ans_start; i <= ans_end; i += 72){
		for(int k = i; k <= min(ans_end,i+71); k++){
			printf("%c",line[k]);
		}
		printf("\n");
	}
	printf("\n");

	line.clear();
	pattern.clear();
}


int main(){

	while(true){
		getline(cin,buf);
		if(buf == "")break;

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 2150
memory_kb: 6068
final_rank: 20

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) begin(v), end(v)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for(int i = (int)(s); i < (int)(n); i++)
#define min(...) min({__VA_ARGS__})
#define max(...) max({__VA_ARGS__})

template<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}
template<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}

using pint = pair<int, int>;
using tint = tuple<int, int, int>;
using vint = vector<int>;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

signed main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  string line;
  while(getline(cin, line), line != "") {
    string s = line, a;
    while(getline(cin, line), line != "") s += line;
    getline(cin, a);

    int n = s.size(), k = a.size();
    vint pos[55];
    rep(i, n) rep(j, k) {
      if(s[i] == a[j]) pos[j].push_back(i);
    }
    bool imp = false;
    rep(i, k) if(!pos[i].size()) imp = true;
    if(imp) {
      cout << 0 << endl << endl;
      getline(cin, line);
      continue;
    }

    int idx[55] = {};
    int ans = 0, head = 0, len = n;
    rep(i, n) {
      int tail = i;
      rep(j, k) chmax(tail, pos[j][idx[j]]);
      if(tail-i+1 < len) ans = 0, head = i, len = tail-i+1;
      if(tail-i+1 == len) ans++;
      int p = a.find(s[i]);
      if(p != -1) {
	idx[p]++;
	if(idx[p] >= pos[p].size()) break;
      }
    }

    string t = s.substr(head, len);
    cout << ans << endl;
    rep(i, t.size()) {
      if(i%72 == 0) cout << endl;
      cout << t[i];
    }
    cout << endl << endl;
    getline(cin, line);
  }

  return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3476
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
string S,K;
const int n = 1<<8;

int getlen(){
  int mx[n],res=1e9;
  memset(mx,-1,sizeof(mx));
  for(int i=0;i<S.size();i++){
    int ch = S[i];
    mx[ch] = max(mx[ch],i);
    int c=0,l=1e9;
    for(int j=0;j<K.size();j++)c+=mx[K[j]]>=0,l=min(l,mx[K[j]]);
    if(c<K.size()) continue;
    res=min(res,i-l);
  }
  return res;
}

int count(int len){
  int cnt[n]={},res=0;
  for(int i=0;i<S.size();i++){
    cnt[S[i]]++;
    int c=0;
    for(int j=0;j<K.size();j++)c+=cnt[K[j]]>0;
    if(c==K.size())res++;
    if(i-len>=0) cnt[S[i-len]]--;
  }
  return res;
}

string getstr(int len){
  int cnt[n]={},res=0;
  for(int i=0;i<S.size();i++){
    cnt[S[i]]++;
    int c=0;
    for(int j=0;j<K.size();j++)c+=cnt[K[j]]>0;
    if(c==K.size()) return S.substr(i-len,len+1);
    if(i-len>=0) cnt[S[i-len]]--;
  }
  return "";
}

int main(){
  while(1){
    S.clear();
    while(1){
      string s; 
      getline(cin,s);
      if(s.empty()) break;
      S+=s;
    }
    if(S.empty())break;
    getline(cin,K);
    
    int len = getlen();
    cout<<count(len)<<endl;
    string str = getstr(len);
    for(int i=0;i<str.size();i++){
      if(i!=str.size()-1&&i%72==0)cout<<endl;
      cout<<str[i];
    }
    cout<<endl;
    if(len<1e9)cout<<endl;
    getline(cin,K);      
  }
  return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 3376
final_rank: 12

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
  string buf;
  while(getline(cin,buf),buf!=""){
    string s=buf;
    while(getline(cin,buf),buf!="") s+=buf;
    string a;
    getline(cin,a);
    //cout<<s<<endl<<a<<endl;
     
    int n=a.size();
    vector<int> pos[n];
    for(int i=0;i<(int)s.size();i++)
      for(int j=0;j<n;j++)
    if(s[i]==a[j]) pos[j].push_back(i);
 
    bool f=1;
    for(int i=0;i<n;i++) f&=!!(pos[i].size());
    if(!f){
      cout<<0<<endl<<endl;
      getline(cin,buf);
      continue;
    }
     
    int idx[n];
    memset(idx,0,sizeof(idx));
     
    int ans=0,len=s.size()+1,head=0;
    for(int i=0;i<(int)s.size();i++){
      int nxt=i;
      for(int j=0;j<n;j++)
    nxt=max(nxt,pos[j][idx[j]]);
 
      if(nxt-i+1<len) ans=0,len=nxt-i+1,head=i;
      if(nxt-i+1==len) ans++;
 
      for(int j=0;j<n;j++){
    if(s[i]==a[j]) idx[j]++;
    if(idx[j]>=(int)pos[j].size()) goto LAB;
      }
    }
     
  LAB:
    string str=s.substr(head,len);
     
    cout<<ans<<endl;
    for(int i=0;i<(int)str.size();i++){
      if(i%72==0) cout<<endl;
      cout<<str[i];
    }
    cout<<endl<<endl;;
    //ready for next;
    getline(cin,buf);
  }
  return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 3608
final_rank: 14

code_snippet: #include<bits/stdc++.h>
#define M 1000005
#define N 300
using namespace std;
string s,t,temp;
int scnt[N],tcnt[N],ans[M],B[M],E[M];

int main(){
  while(1){
    getline(cin,temp);
    if(temp=="")break;
    s="";
    s+=temp;
    while(1){
      getline(cin,temp);
      s+=temp;
      if(temp=="")break;
    }
    getline(cin,temp);
    t=temp;
    getline(cin,temp);
    int flag=0;
    memset(tcnt,0,sizeof(tcnt));
    for(int i=0;i<(int)t.size();i++)
      tcnt[(int)t[i]]++;
    int cnt=0,idx=0;
    memset(scnt,0,sizeof(scnt));
    memset(ans,0,sizeof(ans));  
    for(int i=0;i<(int)s.size();i++){
      if(scnt[(int)s[i]]<tcnt[(int)s[i]])cnt++;
      scnt[(int)s[i]]++;
      if(cnt==t.size()){
	flag=1;
	if(!ans[i-idx+1]){
	  B[i-idx+1]=idx;
	  E[i-idx+1]=i;
	  ans[i-idx+1]++;
	}else ans[i-idx+1]++;
	while(1){
	  if(scnt[(int)s[idx]]==tcnt[(int)s[idx]]){
	    scnt[(int)s[idx]]--;
	    idx++;
	    cnt--;
	    break;
	  }
	  scnt[(int)s[idx]]--;
	  idx++;
	  if(cnt==t.size()){
	    if(!ans[i-idx+1]){
	      B[i-idx+1]=idx;
	      E[i-idx+1]=i;
	      ans[i-idx+1]++;
	    }else ans[i-idx+1]++;
	  }
	}
      }
    }
    if(!flag)cout<<0<<endl<<endl;
    for(int i=0;i<M;i++)
      if(ans[i]){
	cout<<ans[i]<<endl<<endl;
	string str=s.substr(B[i],E[i]-B[i]+1);
	for(int j=0;j<str.size();j++){
	  cout<<str[j];
	  if(!((j+1)%72))cout<<endl;
	}
	if(str.size()%72)cout<<endl;
	cout<<endl;
	break;
      }
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 7508
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
  string buf;
  while(getline(cin,buf),buf!=""){
    string s=buf;
    while(getline(cin,buf),buf!="") s+=buf;
    string a;
    getline(cin,a);
    //cout<<s<<endl<<a<<endl;
    
    int n=a.size();
    vector<int> pos[n];
    for(int i=0;i<(int)s.size();i++)
      for(int j=0;j<n;j++)
	if(s[i]==a[j]) pos[j].push_back(i);

    bool f=1;
    for(int i=0;i<n;i++) f&=!!(pos[i].size());
    if(!f){
      cout<<0<<endl<<endl;
      getline(cin,buf);
      continue;
    }
    
    int idx[n];
    memset(idx,0,sizeof(idx));
    
    int ans=0,len=s.size()+1,head=0;
    for(int i=0;i<(int)s.size();i++){
      int nxt=i;
      for(int j=0;j<n;j++)
	nxt=max(nxt,pos[j][idx[j]]);

      if(nxt-i+1<len) ans=0,len=nxt-i+1,head=i;
      if(nxt-i+1==len) ans++;

      for(int j=0;j<n;j++){
	if(s[i]==a[j]) idx[j]++;
	if(idx[j]>=(int)pos[j].size()) goto LAB;
      }
    }
    
  LAB:
    string str=s.substr(head,len);
    
    cout<<ans<<endl;
    for(int i=0;i<(int)str.size();i++){
      if(i%72==0) cout<<endl;
      cout<<str[i];
    }
    cout<<endl<<endl;;
    //ready for next;
    getline(cin,buf);
  }
  return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 3448
final_rank: 13

code_snippet: /*
000000000011111111112222222222333333333344444444
012345678901234567890123456789012345678901234567
TfefirstemampleistrivivlTfefirstemampleistrifivl
                                   .        . .
len = 18 sp = 3
len = 15 sp = 11
len = 12 sp = 22
len = 12 sp = 35
mfv
  
000000000011111111112222222222
012345678901234567890123456789
aabbccbbccaabbaacceeddffccaabb
       . ..
  
len = 4 sp = 1
  
abc
  
*/
  
#include<set>
#include<iostream>
#include<cmath>
#include<cstdio>
  
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define inf (1<<29)
  
using namespace std;
  
void compute(string text,string S)
{
  int nt = text.size(),nS = S.size();
  int mlen = inf;
  string ans;
  set<string> all;
  bool found[nS];
  int CNT = 0;
  int fpos[nS];
  rep(i,nS)found[i] = false;
  
  rep(i,nt)
    {
      bool update = false;
      int cnt = 0,mn,mx;
      mn = inf,mx = -inf;
      rep(j,nS)
    {
      if(S[j] == text[i])
        {
          update = true;
          found[j] = true;
          fpos[j] = i;
        }
      if(found[j])
        {
          cnt++;
          mn = min(mn,fpos[j]);
          mx = max(mx,fpos[j]);
        }
    }
      if(cnt == nS)
    {
      int cost = mx - mn + 1;
      if(cost < mlen)
        {
          mlen = cost;
          ans = text.substr(mn,cost);
          all.clear();
          CNT = 1;
          all.insert(text.substr(mn,cost));
        }
      else if(cost == mlen)
        {
          if(update)CNT++;
          all.insert(text.substr(mn,cost));
        }
    }
    }
  cout << CNT << endl << endl;
  //printf("%d\n\n",(int)all.size());
   
  if(all.empty())
    {
      return;
    }
 
  rep(i,ans.size())
    if((i+1)%72 == 0 && i != 0)
      printf("%c\n",ans[i]);
    else
      printf("%c",ans[i]);
  if(ans.size()%72 != 0)puts("\n");
  else puts("");
}
  
int main()
{
  string blank;
  while(getline(cin,blank))
    {
      string text,S;
      text += blank;
      while(getline(cin,blank))
    {
      if(blank.empty())break;
      text += blank;
    }
      if(text.empty())goto Fin;
      while(getline(cin,blank))
    if(blank.empty())break;
    else S += blank;
      compute(text,S);
    }
 Fin:;
  return 0;
}
accuracy: 1
time_ms: 550
memory_kb: 2652
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
  while(1) {
    string s="",t,r;
    getline(cin,s);
    if(s=="") break;
    while(1) {
      getline(cin,r);
      if(r=="") break;
      s+=r;
    }
    getline(cin,t);
    getline(cin,r);
    
    map<char,int> m,d;
    m.clear();
    d.clear();
    for(int i=0; i<t.size(); i++) d[t[i]]=1;
    vector<int> v;
    for(int i=0; i<s.size(); i++) {
      if(d[s[i]]==1) v.push_back(i);
    }
    int x=0,cnt=0;
    string ans=s;
    for(int i=0; i<v.size(); i++) {
      char c=s[v[i]];
      m[c]++;
      if(m.size()==t.size()) {
	while(m[s[v[x]]]>1) {
	  m[s[v[x]]]--;
	  x++;
	}
	if(ans.size()==v[i]-v[x]+1) {
	  cnt++;
	} else if(ans.size()>v[i]-v[x]+1) {
	  cnt=1;
	  ans=s.substr(v[x],v[i]-v[x]+1);
	}
      }
    }
    
    if(cnt==0) cout << cnt << endl;
    else {
      cout << cnt << endl << endl;
      for(int i=0; i<ans.size(); i++) {
	cout << ans[i];
	if((i+1)%72==0) cout << endl;
      }
      if(ans.size()%72) cout << endl;
    }
    cout << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 2116
final_rank: 9

code_snippet: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
#include <assert.h>
  
using namespace std;
  
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P> PP;
  
static const double EPS = 1e-8;

void print_text(const string& text){
  int offset = 0;
  int text_i = 0;
  while(offset < text.size()){
    for(text_i = offset; text_i < text.size(); text_i++){
      if(text_i - offset >= 72) break;
      printf("%c",text[text_i]);
    }
    printf("\n");
    offset += 72;
  }
}

void print_co_occurrence(const string& text,const string& query){

  int head = 0;
  int tail = 0;

  int include_keys = 0;
  int freq[256] = {};

  bool is_key[256];
  memset(is_key,false,sizeof(is_key));

  for(int query_i = 0; query_i < query.size(); query_i++){
    is_key[query[query_i]] = true;
  }

  int min_tail = INF;
  int min_len = INF;
  int ans_freq = 0;
  string ans = "";

  while(tail < text.size()){
    for(;head < text.size();head++){
      if(is_key[text[head]] && !freq[text[head]]++){
        include_keys++;
        if(include_keys == query.size()){
          head++;
          break;
        }
      }
    }

    for(;tail <= head;tail++){
      if(is_key[text[tail]] && !--freq[text[tail]]){

        if(include_keys == query.size()) {
          if((head -1) - tail + 1 < min_len){
            min_len = (head -1) - tail + 1;
            ans = text.substr(tail,(head -1) - tail + 1);
            ans_freq = 1;
          }
          else if((head -1) - tail + 1 == min_len){
            ans_freq++;
          }
        }
        include_keys--;
        tail++;
        break;
      }
    }
  }

  if(ans_freq == 0){
    printf("%d\n",ans_freq);
    printf("\n");
  }
  else{
    printf("%d\n",ans_freq);
    printf("\n");
    print_text(ans);
    printf("\n");
  }
}

int main(){
  string str;
  int phase = 0;
  string text = "";
  string query = "";
  while(getline(cin,str)){
    if(str.size() == 0){
      phase++;
      continue;
    }
    if(phase % 2 == 0){
      text += str;
    }
    else{
      query = str;

      print_co_occurrence(text,query);
      
      query = "";
      text = "";
    }
  }
}
accuracy: 1
time_ms: 20
memory_kb: 1844
final_rank: 2

code_snippet: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
#include <assert.h>
  
using namespace std;
  
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P> PP;
  
static const double EPS = 1e-8;

void print_text(const string& text){
  int offset = 0;
  int text_i = 0;
  while(offset < text.size()){
    for(text_i = offset; text_i < text.size(); text_i++){
      if(text_i - offset >= 72) break;
      printf("%c",text[text_i]);
    }
    printf("\n");
    offset += 72;
  }
}

void print_co_occurrence(const string& text,const string& query){

  int head = 0;
  int tail = 0;

  int include_keys = 0;
  int freq[1024] = {};

  bool is_key[1024];
  memset(is_key,false,sizeof(is_key));

  for(int query_i = 0; query_i < query.size(); query_i++){
    is_key[query[query_i]] = true;
  }

  int min_tail = INF;
  int min_len = INF;
  int ans_freq = 0;
  string ans = "";

  while(tail < text.size()){
    for(;head < text.size();head++){
      if(is_key[text[head]] && !freq[text[head]]++){
        include_keys++;
        if(include_keys == query.size()){
          head++;
          break;
        }
      }
    }

    for(;tail <= head;tail++){
      if(is_key[text[tail]] && !--freq[text[tail]]){

        if(include_keys == query.size()) {
          if((head -1) - tail + 1 < min_len){
            min_len = (head -1) - tail + 1;
            ans = text.substr(tail,(head -1) - tail + 1);
            ans_freq = 1;
          }
          else if((head -1) - tail + 1 == min_len){
            ans_freq++;
          }
        }
        include_keys--;
        tail++;
        break;
      }
    }
  }

  if(ans_freq == 0){
    printf("%d\n",ans_freq);
    printf("\n");
  }
  else{
    printf("%d\n",ans_freq);
    printf("\n");
    print_text(ans);
    printf("\n");
  }
}

int main(){
  string str;
  int phase = 0;
  string text = "";
  string query = "";
  while(getline(cin,str)){
    if(str.size() == 0){
      phase++;
      continue;
    }
    if(phase % 2 == 0){
      text += str;
    }
    else{
      query = str;

      print_co_occurrence(text,query);
      
      query = "";
      text = "";
    }
  }
}
accuracy: 1
time_ms: 20
memory_kb: 1848
final_rank: 3

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <numeric>
#include <cctype>
#include <tuple>

#ifdef _MSC_VER
#include <agents.h>
#endif

#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int valid[256];
int cnt[256];
int kind;

void add(char c){
	if (!valid[c]) return;
	if (cnt[c]++) return;
	++kind;
}

void sub(char c){
	if (!valid[c]) return;
	if (--cnt[c]) return;
	--kind;
}

int main(){
	string str, key;
	while (1){
		str.clear();
		key.clear();

		while (true){
			getline(cin, key);
			if (key.empty() || !cin) break;
			str += key;
		}
		if (str.empty() || !cin) break;

		getline(cin, key);
		cin.ignore();

		//cout << str << endl;
		//cout << key << endl;
		MEMSET(cnt, 0);
		MEMSET(valid, 0);
		kind = 0;
		for (auto &c : key) valid[c] = 1;
		int n = str.size(), m = key.size();

		int l = 0, r = 0;
		int ans = n + 1;
		int anscnt = 0;
		P p;
		while (1){
			while (kind < m && r < n){
				add(str[r++]);
			}
			if (kind == m){
				if (r - l < ans){
					ans = r - l;
					p = make_pair(l, r);
					anscnt = 1;
				}
				else if (r - l == ans){
					++anscnt;
				}
			}
			if (l >= r) break;
			sub(str[l++]);
		}

		cout << anscnt << endl;
		int chr = 0;
		if (anscnt){
			cout << endl;
			for (int i = p.first; i < p.second; ++i){
				cout << str[i];
				++chr;
				if (chr % 72 == 0) cout << endl;
			}
			if (chr % 72) cout << endl;
		}
		cout << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 1692
final_rank: 1

code_snippet: #include<iostream>
#include<cmath>
#include<cstdio>
 
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define inf (1<<29)
 
using namespace std;
 
void compute(string text,string S)
{
  int nt = text.size(),nS = S.size();
  int mlen = inf;
  string ans;
  bool found[nS];
  int CNT = 0;
  int fpos[nS];
  rep(i,nS)found[i] = false;
 
  rep(i,nt)
    {
      bool update = false;
      int cnt = 0,mn,mx;
      mn = inf,mx = -inf;
      rep(j,nS)
	{
	  if(S[j] == text[i])
	    {
	      update = true;
	      found[j] = true;
	      fpos[j] = i;
	    }
	  if(found[j])
	    {
	      cnt++;
	      mn = min(mn,fpos[j]);
	      mx = max(mx,fpos[j]);
	    }
	}
      if(cnt == nS)
	{
	  int cost = mx - mn + 1;
	  if(cost < mlen)
	    {
	      mlen = cost;
	      ans = text.substr(mn,cost);
	      CNT = 1;
	    }
	  else if(cost == mlen && update)CNT++;	    
	}
    }
  cout << CNT << endl << endl;
  if(!CNT)return;

  rep(i,ans.size())
    if((i+1)%72 == 0 && i != 0)
      printf("%c\n",ans[i]);
    else
      printf("%c",ans[i]);
  if(ans.size()%72 != 0)puts("\n");
  else puts("");
}
 
int main()
{
  string blank;
  while(getline(cin,blank))
    {
      string text,S;
      text += blank;
      while(getline(cin,blank))
	{
	  if(blank.empty())break;
	  text += blank;
	}
      if(text.empty())goto Fin;
      while(getline(cin,blank))
	if(blank.empty())break;
	else S += blank;
      compute(text,S);
    }
 Fin:;
  return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 2384
final_rank: 10

code_snippet: /*
000000000011111111112222222222333333333344444444
012345678901234567890123456789012345678901234567
TfefirstemampleistrivivlTfefirstemampleistrifivl
                                   .        . .
len = 18 sp = 3
len = 15 sp = 11
len = 12 sp = 22
len = 12 sp = 35
mfv
 
000000000011111111112222222222
012345678901234567890123456789
aabbccbbccaabbaacceeddffccaabb
       . ..
 
len = 4 sp = 1
 
abc
 
*/
 
#include<set>
#include<iostream>
#include<cmath>
#include<cstdio>
 
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define inf (1<<29)
 
using namespace std;
 
void compute(string text,string S)
{
  int nt = text.size(),nS = S.size();
  int mlen = inf;
  string ans;
  set<string> all;
  bool found[nS];
  int CNT = 0;
  int fpos[nS];
  rep(i,nS)found[i] = false;
 
  rep(i,nt)
    {
      bool update = false;
      int cnt = 0,mn,mx;
      mn = inf,mx = -inf;
      rep(j,nS)
	{
	  if(S[j] == text[i])
	    {
	      update = true;
	      found[j] = true;
	      fpos[j] = i;
	    }
	  if(found[j])
	    {
	      cnt++;
	      mn = min(mn,fpos[j]);
	      mx = max(mx,fpos[j]);
	    }
	}
      if(cnt == nS)
	{
	  int cost = mx - mn + 1;
	  if(cost < mlen)
	    {
	      mlen = cost;
	      ans = text.substr(mn,cost);
	      all.clear();
	      CNT = 1;
	      all.insert(text.substr(mn,cost));
	    }
	  else if(cost == mlen)
	    {
	      if(update)CNT++;
	      all.insert(text.substr(mn,cost));
	    }
	}
    }
  cout << CNT << endl << endl;
  //printf("%d\n\n",(int)all.size());
  
  if(all.empty())
    {
      return;
    }

  rep(i,ans.size())
    if((i+1)%72 == 0 && i != 0)
      printf("%c\n",ans[i]);
    else
      printf("%c",ans[i]);
  if(ans.size()%72 != 0)puts("\n");
  else puts("");
}
 
int main()
{
  string blank;
  while(getline(cin,blank))
    {
      string text,S;
      text += blank;
      while(getline(cin,blank))
	{
	  if(blank.empty())break;
	  text += blank;
	}
      if(text.empty())goto Fin;
      while(getline(cin,blank))
	if(blank.empty())break;
	else S += blank;
      compute(text,S);
    }
 Fin:;
  return 0;
}
accuracy: 1
time_ms: 550
memory_kb: 2656
final_rank: 17

code_snippet: #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
using namespace std;
 
#define FOUND 1
#define NOT_FOUND -1
 
bool compare(string s1, string s2){
  if(s1.size() == s2.size()) return s1 < s2;
  return s1.size() < s2.size();
} 
 
int main(){
  string in, sub;
  string seq;
  bool flag = true;
  while(getline(cin, in)){
    if(!flag && (in.size() == 0 || cin.eof())){
      if(cin.eof()) sub += in;   
      flag = true;
       
      map<char, int > mp;
      set<char> data;
 
      for(int i = 0 ; i < sub.size() ; i++) data.insert(sub[i]);
      for(int i = 0 ; i < sub.size() ; i++) mp[ sub[i] ] = NOT_FOUND;
       
      int ans = 0;
      int len = (1<<29);
      string ans2;
       
      for(int i = 0 ; i < seq.size() ; i++){
    if(data.find(seq[i]) == data.end()) continue;
    mp[ seq[i] ] = i;
     
    int left = (1<<29);
    bool ok = true;
    for(int j = 0 ; j < sub.size() ; j++){
      if(mp[ sub[j] ] == NOT_FOUND){
        ok = false;
        break;
      }
      left = min(left, mp[ sub[j] ]);
    }
     
    if(!ok) continue;
     
    if(i - left == len) ans++;
    if(i - left < len){
      len = i - left;
      ans = 1;
      //cout << "i = " << i << " left = " << left << endl;
      //cout << i << endl;
      ans2 = seq.substr(left, i-left+1);
      //cout << seq.substr(left, i-left+1) << endl;
    }
     
      }
       
      cout << ans << endl << endl;
      if(ans != 0){
    for(int i = 0 ; i < ans2.size() ; i += 72){
      cout << ans2.substr(i, 72) << endl;
    }
    if(!cin.eof()) cout << endl;
      }
 
      //cout << "ans_len = " << len << endl;
      //cout << "ans_cnt = " << ans << endl;
      //cout << "seq = " << seq << endl;
      //cout << "sub = " << sub << endl;
      //cout << endl;
      seq = "", sub = "";
      continue;
    }
    else if(flag && in.size() != 0){
      seq += in;
      continue;
    }
    else if(flag && in.size() == 0){
      flag = false;
      continue;
    }
    else if(!flag && in.size() != 0){
      sub += in;
      continue;
    }
   
     
 
  }
   
  return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 1832
final_rank: 5

code_snippet: #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
using namespace std;

#define FOUND 1
#define NOT_FOUND -1

bool compare(string s1, string s2){
  if(s1.size() == s2.size()) return s1 < s2;
  return s1.size() < s2.size();
} 

int main(){
  string in, sub;
  string seq;
  bool flag = true;
  while(getline(cin, in)){
    if(!flag && (in.size() == 0 || cin.eof())){
      if(cin.eof()) sub += in;   
      flag = true;
      
      map<char, int > mp;
      set<char> data;

      for(int i = 0 ; i < sub.size() ; i++) data.insert(sub[i]);
      for(int i = 0 ; i < sub.size() ; i++) mp[ sub[i] ] = NOT_FOUND;
      
      int ans = 0;
      int len = (1<<29);
      string ans2;
      
      for(int i = 0 ; i < seq.size() ; i++){
	if(data.find(seq[i]) == data.end()) continue;
	mp[ seq[i] ] = i;
	
	int left = (1<<29);
	bool ok = true;
	for(int j = 0 ; j < sub.size() ; j++){
	  if(mp[ sub[j] ] == NOT_FOUND){
	    ok = false;
	    break;
	  }
	  left = min(left, mp[ sub[j] ]);
	}
	
	if(!ok) continue;
	
	if(i - left == len) ans++;
	if(i - left < len){
	  len = i - left;
	  ans = 1;
	  //cout << "i = " << i << " left = " << left << endl;
	  //cout << i << endl;
	  ans2 = seq.substr(left, i-left+1);
	  //cout << seq.substr(left, i-left+1) << endl;
	}
	
      }
      
      cout << ans << endl << endl;
      if(ans != 0){
	for(int i = 0 ; i < ans2.size() ; i += 72){
	  cout << ans2.substr(i, 72) << endl;
	}
	if(!cin.eof()) cout << endl;
      }

      //cout << "ans_len = " << len << endl;
      //cout << "ans_cnt = " << ans << endl;
      //cout << "seq = " << seq << endl;
      //cout << "sub = " << sub << endl;
      //cout << endl;
      seq = "", sub = "";
      continue;
    }
    else if(flag && in.size() != 0){
      seq += in;
      continue;
    }
    else if(flag && in.size() == 0){
      flag = false;
      continue;
    }
    else if(!flag && in.size() != 0){
      sub += in;
      continue;
    }
  
    

  }
  
  return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 1832
final_rank: 6

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
#include<string>

#define INF (1<<29)
#define all(c) (c).begin(),(c).end()

using namespace std;


int main(void){

  string s,t,in;
  bool f=false;
  while(true){
    s.clear();
    t.clear();
    while(true){
      getline(cin,in);
      if(in.size()==0)break;
      s+=in;
    }

    while(true){
      getline(cin,in);
      if(in.size()==0)break;
      t+=in;
    }

    if(s.size()==0 && t.size()==0)break;

    //if(f)cout << endl;
    f=true;

    int sl=s.size(),tl=t.size();
    int now[tl],ans=0;
    string res="";

    fill(now,now+tl,INF);

    int tmp=0;
    for(int i=0;i<sl;i++){
      for(int j=0;j<tl;j++){
        if(s[i]==t[j]){
          now[j]=min(now[j],i);
        }
      }
    }

    bool flag=false;
    for(int i=0;i<tl;i++){
      flag|=(now[i]>=INF);
    }
    if(flag){
      cout << 0 << endl << endl;;
      continue;
    }

    int left=INF,right=-1,lid,rid;
    for(int i=0;i<tl;i++){
      if(left>now[i])left=now[i],lid=i;
      if(right<now[i])right=now[i],rid=i;
    }
    ans++;
    res=s.substr(left,right-left+1);

    while(true){
      int mn=INF,id=0;
      for(int i=0;i<tl;i++){
        if(mn>now[i])mn=now[i],id=i;
      }
      while(now[id]<sl && s[++now[id]]!=t[id]);
      if(s[now[id]]!=t[id])break;
      int l=INF,r=0;
      bool fg=true;
      for(int i=0;i<tl;i++){
        l=min(l,now[i]);
        r=max(r,now[i]);
        fg&=(now[i]>=sl-1);
      }
      if(fg)break;
      if(res.size()>r-l+1){
        ans=0;
        res=s.substr(l,r-l+1);
      }
      if(res.size()==r-l+1)ans++;
    }
    cout << ans << endl << endl;
    for(int i=0;i<res.size();i++){
      cout << res[i];
      if((i+1)%72==0)cout << endl;
    }
    if(res.size()%72>0)cout << endl;
    cout << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 1828
final_rank: 8

code_snippet: #include<iostream>
#include<map>
#include<cstring>

using namespace std;

const int MAX = 51;

string text, key;
map<char,int> M;
int numKey[MAX];

typedef long long ll;

bool input(){
  text = "";

  string s = "";
  if(!getline(cin,s)) return false;
  if(s == "") return false;
  text = s;
  while(getline(cin,s)&&s!="") text += s;
  getline(cin,s);
  key = s;
  getline(cin,s); // for blank line
  return true;
}



void solve(){

  memset(numKey,0,sizeof(numKey));

  M.clear();
  for(int i = 0; i < (int)key.length(); i++)
    M[key[i]] = i;

  ll need = (1LL<<key.length())-1;

  string first = "";
  int num = 0;

  int s = 0, t = 0;

  while(s <= t && t <= (int)text.length()){
    if(need == 0){
      // if(text.size() > 400 && t-s < 50) cout << text.substr(s,t-s) << endl;
      if(first != "" && (int)first.length() == t-s){
	num++;
      }
      if(first == "" || (int)first.length() > t-s){
	first = text.substr(s,t-s);
	num = 1;
      }

      if(M.count(text[s]) != 0){
	numKey[M[text[s]]]--;
	if(numKey[M[text[s]]] == 0) need |= (1LL<<M[text[s]]);
      }
      s++;
    }else{
      if(M.count(text[t]) != 0){
	if(need&(1LL<<M[text[t]])) need -= (1LL<<M[text[t]]);
	numKey[M[text[t]]]++;
      }
      t++;
    }
  }
  // cout << "min " << minT << endl;
  if(num == 0) cout << num << endl;
  else{
    cout << num << endl << endl;

    for(int i = 0; i < (int)first.length(); i++){
      cout << first[i];
      if((i+1)%72 == 0) cout<<endl;
    }
    if((int)first.size()%72)cout << endl;
  }
  cout << endl;
}	 

int main(){

  // bool isFirst = true;
  while(input()){
    // if(isFirst) isFirst = false;
    // else cout << endl;
    solve();
  }
  return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 1832
final_rank: 6

code_snippet: #include <iostream>
#include <string>
#include <algorithm>
using namespace std;

string S,K;
const int INF=1<<28;

void solve(){
	string ans;
	int len=INF, cnt=0;
	int left=INF;
	int idx[K.length()];
//	fill(begin(idx), end(idx), 0);
	for(int i=0; i<K.length(); ++i)idx[i]=-1;

	for(int p=0; p<S.length(); ++p){
		for(int k=0; k<K.length(); ++k){
			if(S[p]==K[k]){
				idx[k]=p;
				left=INF;
				for(int i=0; i<K.length(); ++i)	left=min(left, idx[i]);
				if(left>=0){
//					cout << left << " " << p << endl;
					string cand=S.substr(left, p-left+1);
					if(len>cand.length()){
						cnt=1;
						len=cand.length();
						ans=cand;
					}else if(len==cand.length()){
						++cnt;
					}
				}
			}
		}
	}

	if(len>=INF){
		cout << 0 << endl;
	}else{
		cout << cnt << endl;
		cout << endl;

		for(int i=0; i<ans.size(); ++i){
			if(i>0 && i%72==0)	cout<<endl;
			cout << ans[i];
		}
		cout<<endl;
	}
	cout<<endl;
}

int main(){
	for(int cnt=0;;++cnt){
		string tmp;
		if(getline(cin,tmp).eof())	break;
		if(tmp=="")	break;
		S=tmp;
		while(true){
			getline(cin,tmp);
			if(tmp=="")	break;
			S+=tmp;
		}
		getline(cin,K);
		getline(cin,tmp);
//		cout<<S<<endl;
//		cout<<K<<endl;
//		if(cnt>0)	cout<<endl;
		solve();
	}
	return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 1828
final_rank: 4

code_snippet: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

int main(){
  bool begin = true;
  while(true){
    string s, text, query;
    while(getline(cin, s) && s != ""){
      text += s;
    }
    if(text == "") break;
    getline(cin, query);
    assert(query != "");
    getline(cin, s); // empty
    assert(s == "");

    /*
    if(begin) begin = false;
    else cout << endl;
    */

    int N = text.size();
    int L = query.size();

    map<char, int> to_idx;
    REP(i, L) to_idx[query[i]] = i;

    vector<int> idx_vec[100];
    REP(i, N) if(to_idx.count(text[i])) idx_vec[to_idx[text[i]]].push_back(i);

    bool ok = true;
    REP(i, L) if(idx_vec[i].empty()) ok = false;
    if(!ok){
      cout << 0 << endl;
      cout << endl;
      continue;
    }

    map<int, set<int> > cnt;
    int shortest_length = INF;
    vector<int> cur(L);
    while(true){
      int b = idx_vec[0][cur[0]], e = idx_vec[0][cur[0]];
      REP(i, L) {
        b = min(b, idx_vec[i][cur[i]]);
        e = max(e, idx_vec[i][cur[i]]);
      }
      int l = e - b + 1;
      shortest_length = min(shortest_length, l);
      if(shortest_length == l) cnt[l].insert(b);

      int idx = -1;
      REP(i, L)if(cur[i] + 1 < idx_vec[i].size()){
        if(idx == -1 || idx_vec[i][cur[i] + 1] < idx_vec[idx][cur[idx] + 1]){
          idx = i;
        }
      }
      if(idx == -1) break;
      cur[idx] += 1;
    }
    cout << cnt[shortest_length].size() << endl;
    cout << endl;
    string ans = text.substr(*cnt[shortest_length].begin(), shortest_length);
    for(int i = 0; i < ans.size(); i += 72){
      if(i + 72 < ans.size()) cout << ans.substr(i, 72) << endl;
      else cout << ans.substr(i) << endl;
    }
    cout << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3712
final_rank: 15

code_snippet: //47
#include<iostream>
#include<sstream>
#include<string>
#include<map>
#include<algorithm>
#include<set>

using namespace std;

int main(){
  for(string si;getline(cin,si),si!="";){
    string s;
    do{
      s+=si;
      getline(cin,si);
    }while(si!="");
    string k;
    getline(cin,k);
    cin.ignore();
    map<char,int> m;
    int f=0,l=1<<30;
    int n=0;
    set<long long> st;
    for(int i=0;i<s.size();i++){
      if(k.find(s[i])!=string::npos){
	m[s[i]]=i;
      }
      if(m.size()!=k.size())continue;
      int cl=0,cf=1<<30;
      for(int j=0;j<k.size();j++){
	cl=max(cl,m[k[j]]);
	cf=min(cf,m[k[j]]);
      }
      if(!st.insert((long long)cl<<32|cf).second)continue;
      if(cl-cf<=l-f){
	if(cl-cf<l-f){
	  f=cf;
	  l=cl;
	  n=0;
	}
	n++;
      }
    }
    cout<<n<<endl<<endl;
    if(n){
      string t=s.substr(f,l-f+1);
      for(int i=0;i<t.size();i+=72){
	cout<<t.substr(i,72)<<endl;
      }
      cout<<endl;
    }
  }
  return 0;
}
accuracy: 1
time_ms: 280
memory_kb: 3424
final_rank: 18