fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4.  
  5. //参考:ttp://tokyo-ct.net/usr/kosaka/for_students/jissen1/akiyojissen1/kougi17.html
  6.  
  7. std::map<char, int> BMCount(std::string str){
  8. std::map<char, int> R;
  9.  
  10. for (std::size_t i = 1; i < str.size(); i++){
  11. R[str[i - 1]] = str.size() -i;
  12. }
  13. R[str.back()] = str.size();
  14. return R;
  15. }
  16.  
  17. bool Show(std::map<char, int>& m){
  18. for (auto& o : m){
  19. std::cout << o.first << " ->" << o.second << ", ";
  20. }
  21. std::cout<<std::endl;
  22. return true;
  23. }
  24.  
  25. int main(){
  26.  
  27. auto R= BMCount("hello");
  28. Show(R);
  29. R = BMCount("boyer-moore");
  30. Show(R);
  31. R = BMCount("abcdefghijklnmopqrstuvwxyz");
  32. Show(R);
  33. return 0;
  34.  
  35. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
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,