fork download
#include<bits/stdc++.h>

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
#include<unordered_map>
#include<unordered_set>
#include<stdio.h>
#include<stdlib.h>
typedef long long ll;
typedef unsigned long long ull;
#define pi 3.1415926535
#define mod 1000000007    
// #define mod 99999997                 //change when needed

using namespace std;
// using namespace __gnu_pbds;
template <typename T>
T InvMod(T a,T b,T &x,T &y)
{
    if(a==0)
    {
        x=0;
        y=1;
        return b;
    }
    T x1,y1;
    T g=InvMod(b%a,a,x1,y1);
    x=y1-(b/a)*x1;
    y=x1;
    return g;
}
template <typename T>
T gcd(T a,T b)
{
    if(b==0) return a;
    return gcd(b,a%b);
}
template <typename T>
ll mod_exp(T a,T b,ll m)
{
    if(b==0) return 1;
    if(b==1) return a%m;
    T tmp=mod_exp(a,b/2,m);
    if(b%2==1)
    {
        return ((tmp%m*tmp%m)%m*a%m)%m;
    }
    else
    {
        return (tmp%m*tmp%m)%m;
    }
}

//------------------------------------------------------------------------------------------------------------------------------------------------//
struct HASH{
  size_t operator()(const pair<ll,ll>&x)const{
    return hash<long long>()(((long long)x.first)^(((long long)x.second)<<40));
  }
};
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    // cin>>t;
    while(t--)
    {
        string s;
        getline(cin,s);
        string p;
        getline(cin,p);
        int n;
        cin>>n;
        string tmp=p;
        tmp=tmp+'`'+s;
        // cout<<tmp<<"\n";
        vector<int> z(tmp.length(),0);
        int L=0,R=0;
        for (int i=1;i<tmp.length();i++) 
        {
            if(i>R) 
            {
                L=R=i;
                while(R<tmp.length()&&tmp[R-L]==tmp[R]) 
                {
                    R++;
                }
                z[i]=R-L; 
                R--;
            } 
            else 
            {
                int k=i-L;
                if (z[k]<R-i+1) 
                {
                    z[i]=z[k];
                } 
                else 
                {
                    L=i;
                    while (R<tmp.length()&&tmp[R-L]==tmp[R]) 
                    {
                        R++;
                    }
                    z[i]=R-L; 
                    R--;
                }
            }
            // cout<<z[i]<<" ";
        }
        int arr[s.length()+1];
        memset(arr,0,sizeof arr);
        for(int i=p.length()+1;i<tmp.length();i++)
        arr[z[i]]++;
        // cout<<endl;
        int ans=0;
        int cnt=0;
        for(int i=s.length();i>=0;i--)
        {
            // cout<<i<<" "<<arr[i]<<"\n";
            cnt+=arr[i];
            if(cnt>=n)
            {
                ans=i;
                break;
            }
        }
        if(ans==0)
        {
            cout<<"IMPOSSIBLE\n";
        }
        else
        cout<<s.substr(0,ans)<<"\n";
    }
}
Success #stdin #stdout 0s 4164KB
stdin
Standard input is empty
stdout
IMPOSSIBLE