var sourceStr = "BBC ABB ABCF";
var searchStr = "ABC";
function BF_Ordinary(sourceStr, searchStr) {
var sourceLength = sourceStr.length;
var searchLength = searchStr.length;
var padding = sourceLength - searchLength; //循环的次数
//BBC ABB ABCF =>ABC => 搜索9次
for (var i = 0; i <= padding; i++) {
//如果满足了第一个charAt是相等的
//开始子循环检测
//其中sourceStr的取值是需要叠加i的值
if (sourceStr.charAt(i) == searchStr.charAt(0)) {
//匹配成功的数据
var complete = searchLength;
for (var j = 0; j < searchLength; j++) {
if (sourceStr.charAt(i + j) == searchStr.charAt(j)) {
--complete
if (!complete) {
return i;
}
}
}
}
}
return -1;
}
function BF_Optimize(sourceStr, searchStr) {
var mainLength = sourceStr.length;
var searchLength = searchStr.length;
var padding = mainLength - searchLength;
for (var offset = 0; offset <= padding; offset++) {
var match = true;
for (var i = 0; i < searchLength; i++) {
//取反,如果只要不相等
if (searchStr.charAt(i) !== sourceStr.charAt(offset + i)) {
match = false;
break;
}
}
if (match) return offset;
}
return -1;
}
function BF_Optimize_2(sourceStr, searchStr) {
var i = 0,
j = 0;
while (i < sourceStr.length) {
// 两字母相等则继续
if (sourceStr.charAt(i) == searchStr.charAt(j)) {
i++;
j++;
} else { // 两字母不等则角标后退重新开始匹配
i = i - j + 1; // i 回退到上次匹配首位的下一位
j = 0; // j 回退到子串的首位
}
if (j == searchStr.length) {
return i - j;
}
}
}
<!doctype html>由于电脑性能的不断提高,测试的数据量的大小,可能会导致得到的结果不太准确;<script type="text/javascript">
/////////
//暴力算法 //
//普通版
/////////
function BF_Ordinary(sourceStr, searchStr) {
var sourceLength = sourceStr.length;
var searchLength = searchStr.length;
var padding = sourceLength - searchLength; //循环的次数
//BBC ABB ABCF =>ABC => 搜索9次
for (var i = 0; i <= padding; i++) {
//如果满足了第一个charAt是相等的
//开始子循环检测
//其中sourceStr的取值是需要叠加i的值
if (sourceStr.charAt(i) == searchStr.charAt(0)) {
//匹配成功的数据
var complete = searchLength;
for (var j = 0; j < searchLength; j++) {
if (sourceStr.charAt(i + j) == searchStr.charAt(j)) {
--complete
if (!complete) {
return i;
}
}
}
}
}
return -1;
}
/////////
//暴力算法 //
//优化版
/////////
function BF_Optimize_1(sourceStr, searchStr) {
var mainLength = sourceStr.length;
var searchLength = searchStr.length;
var padding = mainLength - searchLength;
for (var offset = 0; offset <= padding; offset++) {
var match = true;
for (var i = 0; i < searchLength; i++) {
//取反,如果只要不相等
if (searchStr.charAt(i) !== sourceStr.charAt(offset + i)) {
match = false;
break;
}
}
if (match) return offset;
}
return -1;
}
////////
//优化版 //
//while
////////
function BF_Optimize_2(sourceStr, searchStr) {
var i = 0,
j = 0;
while (i < sourceStr.length) {
// 两字母相等则继续
if (sourceStr.charAt(i) == searchStr.charAt(j)) {
i++;
j++;
} else { // 两字母不等则角标后退重新开始匹配
i = i - j + 1; // i 回退到上次匹配首位的下一位
j = 0; // j 回退到子串的首位
}
if (j == searchStr.length) {
return i - j;
}
}
}
/////////
//暴力算法
//递归版本
/////////
function BF_Recursive(sourceStr, searchStr, offset) {
var mainLength = sourceStr.length;
var searchLength = searchStr.length;
if (searchLength > mainLength - offset) {
return -1;
}
offset = offset || 0;
for (var i = 0; searchLength > i; i++) {
if (searchStr.charAt(i) !== sourceStr.charAt(offset + i)) {
return BF_Recursive(sourceStr, searchStr, offset + 1)
}
}
return offset;
}
var sourceStr = "There are some times wThere are some times when clicking “like” on a friend's Facebook status doesn't feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg's radar for a while, he said. In 2010, he told ABC News' Diane Sawyer that that Facebook would “definitely thinkThere are some times when clicking “like” on a friend's Facebook status doesn't feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg's radar for a while, he said. In 2010, he told ABC News' Diane Sawyer that that Facebook would “definitely thinkThere are some times when clicking “like” on a friend's Facebook status doesn't feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg's radar for a while, he said. In 2010, he told ABC News' Diane Sawyer that that Facebook would “definitely thinkThere are some times when clicking “like” on a friend's Facebook status doesn't feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg's radar for a while, he said. In 2010, he told ABC News' Diane Sawyer that that Facebook would “definitely thinkThere are some times when clicking “like” on a friend's Facebook status doesn't feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg's radar for a while, he said. In 2010, he told ABC News' Diane Sawyer that that Facebook would “definitely thinkThere are some times when clicking “like” on a friend's Facebook status doesn't feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg's radar for a while, he said. In 2010, he told ABC News' Diane Sawyer that that Facebook would “definitely thinkhen clicking “like” on a friend's Facebook status doesn't feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg's radar for a while, he said. In 2010, he told ABC News' Diane Sawyer There are some times when clicking “like” on a friend's Facebook status doesn't feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg's radar for a while, he said. In 2010, he told ABC News' Diane Sawyer that that Facebook would “definitely thinkThere are some times when clicking “like” on a friend's Facebook status doesn't feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg's radar for a while, he said. In 2010, he told ABC News' Diane Sawyer that that Facebook would “definitely thinkThere are some times when clicking “like” on a friend's Facebook status doesn't feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg's radar for a while, he said. In 2010, he told ABC News' Diane Sawyer that that Facebook would “definitely thinkThere are some times when clicking “like” on a friend's Facebook status doesn't feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg's radar for a while, he said. In 2010, he told ABC News' Diane Sawyer that that Facebook would “definitely think that that Facebook would “definitely think about” adding a dislike button. “People definitely seem to want it,” Zuckerberg said. Four years later — Zuckerberg says Facebook is still “thinking about” adding the oft-requested sdfafd button, Zuckerberg says Facebook is still “thinking about” adding the oft-requested button. At a town hall meeting on Thursday, the CEO revealed he has some reservations about the feature. “There are two things that it can mean,” Zuckerberg said of the potential button, which could be used in a mean spirited way or to express empathy. Finding how to limit it to the latter is the challenge. Zuckerberg said he doesn't want the button to turn into a “voting mechanism” or something that isn't “socially valuable.” “Often people will tell us they don't feel comfortable pressing ‘like,'” Zuckerberg said. “What's the right way to make it so people can easier express a wide range of emotions?” One suggestion percolating online: Aaron Roll out the feature under a different name. However, an “empathy button” just may not have the same ring to it as “dislike.”";
var searchStr = "adding the oft-requested sdf";
function show(bf_name,fn) {
var myDate = +new Date()
var r = fn();
var div = document.createElement('div')
div.innerHTML = bf_name +'算法,搜索位置:' + r + ",耗时" + (+new Date() - myDate) + "ms";
document.body.appendChild(div);
}
show('BF_Ordinary',function() {
return BF_Ordinary(sourceStr, searchStr)
})
show('BF_Optimize_1',function() {
return BF_Optimize_1(sourceStr, searchStr)
})
show('BF_Optimize_2',function() {
return BF_Optimize_2(sourceStr, searchStr)
})
show('BF_Recursive',function() {
return BF_Recursive(sourceStr, searchStr)
})
</script>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有