百度面试题:找出给定字符串对应的序号
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);
注意到每满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);
相关文章推荐
- 找出给定字符串对应的序号...
- [百度面试题]找出字符串中连续出现次数最多的子串
- 剑指Offer 面试题35:第一个只出现一次的字符 解题报告(华为OJ034-找出字符串中第一个只出现一次的字符)
- 找出给定字符串中数字字符 (即’0’—‘9’这10个数字)的个数
- [经典面试题][百度]电话号码对应英语单词
- 笔试题&面试题:给定n个数,要求比较次数1.5n同时找出最大值和最小值
- [经典面试题][百度]在由N个正整数的集合S中,找出最大元素C,满足C=A + B
- 百度面试题 求字符串中不含重复字符的最长子串长度
- 找出给定字符串中大写字符(即'A'-'Z')的个数
- 【面试题】找出字符串中连续出现次数最多的子串
- 题目描述 给定一个字符串,找出该字符串的最长回文子串。回文字符串指的就是从左右两边看都一样的字符串,如aba,cddc都是回文字符串。字符串abbacdc存在的回文子串有abba和cdc,因此它的最长
- 找出给定字符串中出现最多的字符和次数
- 找出给定字符串中出现最多的字符和次数
- 百度面试题字符串筛选
- 在给定字符串中找出单词
- 【每日面试题】给定一个字符串,求出其最长的重复子串
- 百度面试题:找出数组中出现次数超过一半的数
- 给定两个字符串,一个模式串,一个目标串,找出目标串中符合模式串格式的字串
- c#面试题:找出字符串中出现次数最多的字符及出现次数
- 百度面试题之找出数组中之出现一次的两个数(异或的巧妙应用)