Language: C++

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <utility>
#include <deque>
#include <stack>
#include <bitset>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
#define rep(i,n) for (int i=0;i<n;++i)
#define REP(i,n) for (int i=1;i<=n;++i)
#define all(x) x.begin(),x.end()
#define mp make_pair
#define pb push_back
#define pf push_front
#define F first
#define S second
#define read(x) scanf("%d",&x)
typedef bitset<1000005> bs;
int n,k;
string s[2005];
char S[1000005];
int Len=0;
bool can[2005][10005];
int len[2005],index[2005];
bs segtree[64];
bs state;
inline int getBest(){
	int u=1;
	while(u<32){
		if ((state&segtree[u*2]).none()) u=u*2+1;
		else u*=2;
	}
	return (char)(u-32+'a');
}
int main(){
	ios::sync_with_stdio(false);
	cin>>n>>k;
	for (int i=1;i<=n;++i) cin>>s[i];
	for (int i=1;i<=n;++i) len[i]=s[i].size();
	can[n][k]=1;
	for (int i=n;i>=1;--i){
		for (int j=k;j>=len[i];--j) if (can[i][j]) can[i-1][j]=can[i-1][j-len[i]]=1;
	}
	for (int i=1;i<=n;++i){
		for (int j=0;j<len[i];++j){
			S[Len+j]=s[i][j];
		}
		index[i]=Len;//beginning pos
		Len+=len[i];
	}
	for (int i=0;i<64;++i) segtree[i].reset();
	for (int i=0;i<Len;++i) segtree[S[i]-'a'+32].set(i);
	for (int i=31;i>0;--i) segtree[i]=segtree[i<<1]|segtree[i<<1|1];
	state.reset();
	state.set(0);
	for (int i=0;i<k;++i){// alrady got i chars
		for (int j=1;j<n;++j) if (state[index[j]]) state.set(index[j+1]);
		for (int j=1;j<=n;++j) if (i+len[j]>k||!can[j][i+len[j]]) state.reset(index[j]);
		char nxt=getBest();
		cout<<nxt;
		state&=segtree[nxt-'a'+32];
		state<<=1;// move on
	}
	cout<<endl;
	return 0; 
}

Compiler output:
a.cc:38:25: error: 'int index [2005]' redeclared as different kind of entity
   38 | int len[2005],index[2005];
      |                         ^
In file included from /usr/include/string.h:462,
                 from /usr/include/c++/14/cstring:43,
                 from a.cc:4:
/usr/include/strings.h:50:20: note: previous declaration 'const char* index(const char*, int)'
   50 | extern const char *index (const char *__s, int __c)
      |                    ^~~~~
a.cc: In function 'int main()':
a.cc:62:22: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
   62 |                 index[i]=Len;//beginning pos
      |                      ^
a.cc:71:54: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
   71 |                 for (int j=1;j<n;++j) if (state[index[j]]) state.set(index[j+1]);
      |                                                      ^
a.cc:71:75: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
   71 |                 for (int j=1;j<n;++j) if (state[index[j]]) state.set(index[j+1]);
      |                                                                           ^
a.cc:72:92: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
   72 |                 for (int j=1;j<=n;++j) if (i+len[j]>k||!can[j][i+len[j]]) state.reset(index[j]);
      |                                                                                            ^