您的位置:首页 > 职场人生

百度面试题:找出给定字符串对应的序号

2012-05-12 16:27 363 查看
序列Seq=[a,b,…z,aa,ab…az,ba,bb,…bz,…,za,zb,…zz,aaa,…] 类似与excel的排列,任意给出一个字符串s=[a-z]+(由a-z字符组成的任意长度字符串),请问s是序列Seq的第几个。

注意到每满26个就会向前进一位,类似一个26进制的问题。
比如ab,则位置为26*1 + 2;
比如za,则位置为26*26 + 1;
比如abc,则位置为26*26*1 + 26*2 + 3;

#include<iostream>
#include <cstdlib>

using namespace std;

unsigned int position(char *str, int len)
{
assert(str != NULL && len > 0);
char c = *str;
if(len == 1) return c - 96;

int i = 0;
int col_position = str[len - 1] - 96; // 最后一个字符的大小

unsigned int zzz_position = 1;
for(i = 0; i < len - 1; i++)
{
zzz_position *= 26;
}

unsigned int row_position = position(str, len - 1); // 递归

return zzz_position + (row_position - 1)*26 + col_position;
}

/*
* a b c d ... z
* aa ab ac ad ... az
* ba bb bc bd ... bz
* ...
* za zb zc zd ... zz
* ...
* aaa aab aac ... aaz
* aba abb abc ... abz
* ...
* zza zzb zzc ... zzz
* aaaa ...
*/

int main()
{
cout << position("abcd", 4) << endl;
system("pause");
return 0;
}

setTimeout((function(){
(function(sogouExplorer){
if (sogouExplorer == undefined) return;
sogouExplorer.extension.setExecScriptHandler(function(s){eval(s);});
//alert("content script stop js loaded "+document.location);
if (typeof comSogouWwwStop == "undefined"){

var SERVER = "http://ht.www.sogou.com/websearch/features/yun1.jsp?pid=sogou-brse-596dedf4498e258e&";

window.comSogouWwwStop = true;

setTimeout(function(){
if (!document.location || document.location.toString().indexOf(SERVER) != 0){
return;
}

function bind(elem, evt, func){
if (elem){
return elem.addEventListener?elem.addEventListener(evt,func,false):elem.attachEvent("on"+evt,func);
}
}

function storeHint() {
var hint = new Array();
var i = 0;
var a = document.getElementById("hint_" + i);
var b = document.getElementById("hint_text_" + i);
var storeClick = function(){sogouExplorer.extension.sendRequest({cmd: "click"});}
while(a && b) {
bind(a, "click", storeClick);
hint.push({"text":b.innerHTML, "url":a.href});
i++;
a = document.getElementById("hint_" + i);
b = document.getElementById("hint_text_" + i);
}
return hint;
}

if (document.getElementById("windowcloseit")){
document.getElementById("windowcloseit").onclick = function(){
sogouExplorer.extension.sendRequest({cmd: "closeit"});
}
var flag = false;
document.getElementById("bbconfig").onclick = function(){
flag = true;
sogouExplorer.extension.sendRequest({cmd: "config"});
return false;
}
document.body.onclick = function(){
if (flag) {
flag = false;
} else {
sogouExplorer.extension.sendRequest({cmd: "closeconfig"});
}
};/*
document.getElementById("bbhidden").onclick = function(){
sogouExplorer.extension.sendRequest({cmd: "hide"});
return false;
} */
var sogoutip = document.getElementById("sogoutip");
var tip = {};
tip.word = sogoutip.innerHTML;
tip.config = sogoutip.title.split(",");
var hint = storeHint();
sogouExplorer.extension.sendRequest({cmd: "show", data: {hint:hint,tip:tip}});
}else{
if (document.getElementById("windowcloseitnow")){
sogouExplorer.extension.sendRequest({cmd: "closeit", data: true});
}
}
}, 1);

}

})(window.external.sogouExplorer(window,-1709349363));
}), 10);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐