Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
vector<string> v;
string s;
getline(cin , s);
string st = "";
for(int i = 0 ; i<s.length() ; i++){
if(s[i] == ' '){
v.emplace_back(st);
st = "";
}
else{
st+=s[i];
}
}
v.emplace_back(st);
map<string , int> mp;
int n;
cin>>n;
vector<string> str;
int query = n;
while(n--){
string x;
cin>>x;
str.emplace_back(x);
}
int l = 0;
int minlength = 250001;
for(int i = 0 ; i<v.size() ; i++){
bool atlast = false;
for(int j = 0 ; j<str.size() ; j++){
if(str[j] == v[i]){
mp[str[j]] = i;
if(i == v.size()-1)
atlast = true;
break;
}
}
if(mp.size() == query){
set<int> setpos;
for(auto it = mp.begin() ; it!=mp.end() ; it++){
setpos.insert(it->second);
}
for(int it = *setpos.begin() ; it<=*setpos.rbegin() ; it++){
if(it!=*setpos.rbegin()){
l+=v[it].length()+1;
}
else
l+=v[it].length();
}
minlength = min(l , minlength);
l = 0;
}
}
if(minlength == 250001)
minlength = -1;
cout<<minlength;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment