count the occurences of anagrams
2013-11-11 01:00
375 查看
// count the occurences of anagrams.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <map> using namespace std; int countTheAnagrams(const string &orgString, const string &anagrams) { std::map<char, int> hashMap; std::map<char, int> curMap; for (int i = 0; i < anagrams.size(); i++) { if (hashMap.count(anagrams.at(i)) == 0) { hashMap[anagrams.at(i)] = 1; } else { hashMap[anagrams.at(i)]++; } } int curMatchCount = 0; int retMatchCount = 0; for (int i = 0; i < orgString.size(); i++) { if (hashMap[orgString.at(i)] != 0) { if (curMap.count(orgString.at(i)) == 0) { curMap[orgString.at(i)] = 1; curMatchCount++; } else { if (curMap[orgString.at(i)] < hashMap[orgString.at(i)]) { curMap[orgString.at(i)]++; curMatchCount++; } } if (curMatchCount == anagrams.size()) { curMatchCount = 0; retMatchCount++; curMap.clear(); } } } return retMatchCount; } int _tmain(int argc, _TCHAR* argv[]) { string orgString = "forxxorfxdofr"; string anagrams = "for"; int ret = countTheAnagrams(orgString, anagrams); printf("ret = %d", ret); return 0; }
相关文章推荐
- HDU2196 - Computer(树形DP)
- 如何成为一个牛逼的C/C++程序员?
- fal 失败 无法读取丢失的日志
- 第三章 jQuery中的DOM操作
- 机器学习&数据挖掘笔记_16(常见面试之机器学习算法思想简单梳理)
- Cocos2d-x 2.x:CCTableView的使用
- p45-001
- Android实现导航菜单左右滑动效果
- 图纸网站整改意见.doc
- android多选联系人实现
- SmartUpload类实现上传和下载
- 茶盘网整改意见
- Concrete maths reading notes 1 - ch. 1&2
- c++中WaitForSingleObject函数解析
- myeclipse 遇到的一些问题及解决方案
- C++中const用法总结
- c# in depth之泛型的类型约束详细
- 10g Data Guard physical standby的Bug 5526409(Fail to queue the whole FAL gap)
- CISCO2691的OSPF点对点密文测评测试
- 三种经典网页音乐播放器