s*******f 发帖数: 1114 | 1 This is O(n). Hard to explain, but i think it deserve to go through it with
your test case.
//zzzz码遍本版,回报本版zzzz
//在一个大串中查找和另外一个字符串是anagram的子串:
//GetAnagram("abcdbcsdaqdbahs", "scdcb") ==> "cdbcs"
string GetAnagram(const char *s, const char *sub){
int ls = strlen(s);
int lsub = strlen(sub);
if (ls < lsub || lsub < 1)
return "";
int mp[256];
memset(mp, 0, 256 * sizeof(int));
while (*sub){
++mp[*sub++];
}
const char *p = s;
int count = 0;
whil... 阅读全帖 |
|