Skip to content

Instantly share code, notes, and snippets.

@KT-Yeh
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save KT-Yeh/9251367 to your computer and use it in GitHub Desktop.

Select an option

Save KT-Yeh/9251367 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
int N;
string str;
while (cin >> N) {
cin.get();
getline(cin, str);
map<string, int> Map;
for (int i = 0; i <= str.size()-N; ++i) {
string tmp = str.substr(i, N); // 從第i個位置擷取str N個字元
if (!Map[tmp]) Map[tmp] = 1;
else Map[tmp]++;
}
int Max = 0;
for (auto &m : Map) { // m為pair structure
if (m.second > Max) {
Max = m.second;
str = m.first;
}
}
cout << str << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment