#include <iostream> #include <map> #include <string> //参考:ttp://tokyo-ct.net/usr/kosaka/for_students/jissen1/akiyojissen1/kougi17.html std::map<char, int> BMCount(std::string str){ std::map<char, int> R; for (std::size_t i = 1; i < str.size(); i++){ R[str[i - 1]] = str.size() -i; } R[str.back()] = str.size(); return R; } bool Show(std::map<char, int>& m){ for (auto& o : m){ std::cout << o.first << " ->" << o.second << ", "; } std::cout<<std::endl; return true; } int main(){ auto R= BMCount("hello"); Show(R); R = BMCount("boyer-moore"); Show(R); R = BMCount("abcdefghijklnmopqrstuvwxyz"); Show(R); return 0; }
Standard input is empty
e ->3, h ->4, l ->1, o ->5, - ->5, b ->10, e ->11, m ->4, o ->2, r ->1, y ->8, a ->25, b ->24, c ->23, d ->22, e ->21, f ->20, g ->19, h ->18, i ->17, j ->16, k ->15, l ->14, m ->12, n ->13, o ->11, p ->10, q ->9, r ->8, s ->7, t ->6, u ->5, v ->4, w ->3, x ->2, y ->1, z ->26,