UVa 902 - Password Search Solution

#include <iostream>
#include <string>
#include <map>
using namespace std;
int main(){
    int n;
    string s;
    while(cin>>n>>s){
        map<string,int> mp;
        int mx=0;
        string mxs;
        for(int i=0;i<s.size()-n+1;i++){
            string tmp=s.substr(i,n);
            if(mp.find(tmp)==mp.end())
                mp[tmp]=1;
            else
                mp[tmp]+=1;
            if(mp[tmp]>mx){
                mx=mp[tmp];
                mxs=tmp;
            }
        }
        cout<<mxs<<endl;
    }
}

Comments