Created
April 17, 2020 06:51
-
-
Save zarc1411/fe55cfa6f320150a2def48839a64679d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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