返回顶部

牛客练习赛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;
    }
    
posted @ 2020-10-02 21:21  _Kolibri  阅读(173)  评论(0)    收藏  举报