UVa 902 Password Search

解題:利用map存各個Substring的出現率,找最大的 

 1  #include <stdio.h>
 2  #include <iostream>
 3  #include <algorithm>
 4  #include <string>
 5  #include <vector>
 6  #include <map>
 7  using namespace std;
 8  int main()
 9  {
10      int n;
11      while(cin>>n)
12      {
13          string str,sub;
14          map<string,int> m;
15          cin>>str;
16          int len=str.length();
17          for(int i=0; i<=len-n; i++)
18          {
19              sub=str.substr(i,n);
20              if(!m[sub]) m[sub]=1;
21              else m[sub]+=1;
22          }
23          string ans;
24          int maxn=0;
25          for(auto &mi:m)
26          {
27              if(mi.second>maxn)
28              {
29                  maxn=mi.second;
30                  ans=mi.first;
31              }
32          }
33          cout << ans << endl;
34      }
35      return 0;

36  }

留言

這個網誌中的熱門文章

Things a Little Bird Told Me: Confessions of the Creative Mind

UVa 12970 Alcoholic Pilots

UVa 483 Word Scramble