Language: C++

#include <algorithm>
#include <iostream>
#include <string>

template<class K>
class AvlNode
{
public:
	int height;
	int size;
	K key;
	AvlNode<K>* lch;
	AvlNode<K>* rch;
	AvlNode(K key) : key(key), height(1) { }
	~AvlNode<K>() { }
	static AvlNode<K>* update(AvlNode<K>* r)
	{
		if (!r) return nullptr;
		r->height = std::max((r->lch ? r->lch->height : 0), (r->rch ? r->rch->height : 0)) + 1;
		r->size = (r->lch ? r->lch->size : 0) + (r->rch ? r->rch->size : 0) + 1;
		return r;
	}
	static AvlNode<K>* rotateLeft(AvlNode<K>* r)
	{
		AvlNode<K>* p = r->rch;
		r->rch = p->lch;
		p->lch = update(r);
		return update(p);
	}
	static AvlNode<K>* rotateRight(AvlNode<K>* r)
	{
		AvlNode<K>* p = r->lch;
		r->lch = p->rch;
		p->rch = update(r);
		return update(p);
	}
	static int bias(AvlNode<K>* r)
	{
		return r ? ((r->rch ? r->rch->height : 0) - (r->lch ? r->lch->height : 0)) : 0;
	}
	static AvlNode<K>* balance(AvlNode<K>* r)
	{
		int bias = AvlNode<K>::bias(r);
		if (bias > 1) {
			// 右のほうが高い場合
			if (AvlNode<K>::bias(r->rch) < 0) {
				// 子は左の方が高い場合
				r->rch = rotateRight(r->rch);
			}
			r = rotateLeft(r);
		} else if (bias < -1) {
			// 左のほうが高い場合
			if (AvlNode<K>::bias(r->lch) > 0) {
				// 子は右の方が高い場合
				r->lch = rotateLeft(r->lch);
			}
			r = rotateRight(r);
		}
		return r;
	}
	static AvlNode<K>* find(AvlNode<K>* n, K key)
	{
		if (!n) return nullptr;
		if (key < n->key) return find(n->lch, key);
		else if (key > n->key) return find(n->rch, key);
		else return n;
	}
	static AvlNode<K>* minNode(AvlNode<K>* r)
	{
		if (!r) return nullptr;
		while (r->lch) r = r->lch;
		return r;
	}
	static void clearAll(AvlNode<K>* r)
	{
		if (!r) return;
		clearAll(r->lch);
		clearAll(r->rch);
		delete r;
	}
	template<class K>
	static AvlNode<K>* at(AvlNode<K>* r, int index)
	{
		int i = r->lch ? r->lch->size : 0;
		if (index < i) return at(r->lch, index);
		if (i < index) return at(r->rch, index - i - 1);
		return r;
	}
	template<class K>
	static AvlNode<K>* insert(AvlNode<K>* n, K key)
	{
		if (!n) return new AvlNode(key);
		if (key < n->key) n->lch = insert(n->lch, key);
		else if(key > n->key) n->rch = insert(n->rch, key);
		else return;
		return balance(update(n));
	}
	template<class K>
	static AvlNode<K>* erase(AvlNode<K>* n, K key)
	{
		if (!n) return nullptr;
		if (key < n->key) n = AvlNode<K>::erase(n->lch, key);
		else if (key > n->key) n = AvlNode<K>::erase(n->rch, key);
		else {
			if (n->lch && n->rch) {
				AvlNode<K>* min = AvlNode<K>::minNode(n->rch);
				n->key = min->key;
				n->rch = AvlNode<K>::erase(n->rch, min->key);
			} else if (n->lch) {
				AvlNode<K>* ret = n->lch;
				delete n;
				return balance(update(ret));
			} else if (n->rch) {
				AvlNode<K>* ret = n->rch;
				delete n;
				return balance(update(ret));
			} else {
				delete n;
				return nullptr;
			}
		}
		return balance(update(n));
	}
};

template<class K>
class AvlTree :
{
	AvlNode<K>* root;
public:
	AvlTree<K>() { }
	~AvlTree<K>()
	{
		AvlNode<K>::clearAll(root);
	}
	bool contains(K key)
	{
		return AvlNode<K>::find(this->root, key);
	}
	void erase(K key)
	{
		root = AvlNode<K>::erase(this->root, key);
	}
	void insert(K key)
	{
		root = AvlNode<K>::insert(this->root, key);
	}
	K at(int index)
	{
		AvlNode<K>* node = AvlNode<K>::at(this->root, index);
		return node->key;
	}
};

using namespace std;

int main()
{
	string s;
	int k;
	cin >> s >> k;
	AvlTree<string> tree;

	for(int i = 0; i < s.length(); ++i){
	for(int j = i; j < s.length(); ++j){
		tree.insert(s.substr(i, j));
	}
	}

	cout << tree.at(k);

	return 0;
}


Compiler output:
a.cc:81:18: error: declaration of template parameter 'K' shadows template parameter
   81 |         template<class K>
      |                  ^~~~~
a.cc:5:10: note: template parameter 'K' declared here
    5 | template<class K>
      |          ^~~~~
a.cc:89:18: error: declaration of template parameter 'K' shadows template parameter
   89 |         template<class K>
      |                  ^~~~~
a.cc:5:10: note: template parameter 'K' declared here
    5 | template<class K>
      |          ^~~~~
a.cc:98:18: error: declaration of template parameter 'K' shadows template parameter
   98 |         template<class K>
      |                  ^~~~~
a.cc:5:10: note: template parameter 'K' declared here
    5 | template<class K>
      |          ^~~~~
a.cc: In static member function 'static AvlNode<K>* AvlNode<K>::insert(AvlNode<K>*, K)':
a.cc:95:22: error: return-statement with no value, in function returning 'AvlNode<K>*' [-fpermissive]
   95 |                 else return;
      |                      ^~~~~~
a.cc: At global scope:
a.cc:128:1: error: expected class-name before '{' token
  128 | {
      | ^
a.cc: In instantiation of 'static AvlNode<K>* AvlNode<K>::insert(AvlNode<K>*, K) [with K = std::__cxx11::basic_string<char>; K = std::__cxx11::basic_string<char>]':
a.cc:146:28:   required from 'void AvlTree<K>::insert(K) [with K = std::__cxx11::basic_string<char>]'
  146 |                 root = AvlNode<K>::insert(this->root, key);
      |                        ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
a.cc:166:14:   required from here
  166 |                 tree.insert(s.substr(i, j));
      |                 ~~~~~~~~~~~^~~~~~~~~~~~~~~~
a.cc:95:22: error: return-statement with no value, in function returning 'AvlNode<std::__cxx11::basic_string<char> >*' [-fpermissive]
   95 |                 else return;
      |                      ^~~~~~