牛客练习赛70 A.重新排列 (,字符串思维)

-
题意:有一个模板串,给你\(T\)个字符串,选取最短的子串,使其重新排列后包含模板串,求最短的子串的长度
-
题解:遍历字符串,记录每个字符出现的最后位置,每记录一个后再遍历子串,找到子串需要的所有的字符的最后出现的最小位置,如果合法,更新答案即可.
-
代码:
const string tp="puleyaknoi"; int t; string s; map<char,int> last; int res; int main() { ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); cin>>t; while(t--){ cin>>s; res=INF; last.clear(); for(int i=0;i<s.size();++i){ last[s[i]]=i; int cnt=INF; for(auto w:tp){ cnt=min(cnt,last[w]); } if(cnt) res=min(res,i-cnt+1); } if(res==INF) cout<<-1<<endl; else cout<<res<<endl; } return 0; }
𝓐𝓬𝓱𝓲𝓮𝓿𝓮𝓶𝓮𝓷𝓽 𝓹𝓻𝓸𝓿𝓲𝓭𝓮𝓼 𝓽𝓱𝓮 𝓸𝓷𝓵𝔂 𝓻𝓮𝓪𝓵
𝓹𝓵𝓮𝓪𝓼𝓾𝓻𝓮 𝓲𝓷 𝓵𝓲𝓯𝓮

浙公网安备 33010602011771号