您的位置:首页 > 其它

运用字符串处理相关

2014-10-10 15:33 101 查看

题目1.反转单词。输入hello world。输出world hello

将hello world整体做一次strrev(),变成dlrow olleh

再每个单词strrev(),变成world hello

 

题目2.左旋N位字符。输入abcdef和2。 输出cdefab

将输入的前N位做strrev()操作,得到bacdef

再对后len()-N位做strrev()操作,得到bafedc

最后对整体做strrev()操作,得到cdefab

 

其实两次strrev()相当于是换位的作用。

 

题目3.将字符串中的空格替换成%20

 

从后往前复制,每个字符最多只移动一次就OK了,减少了重复的移动。

 

题目4.字符串的全排列

int permutation(char* str,int begin){
int len = strlen(str);
char temp;
int i;
int result = 0;
if(len<1){
return -1;
}
if(begin==len-1){
printf("%s\r\n",str);
return 1;
}
for( i=begin;i<strlen(str);i++){
temp = str[i];
str[i] = str[begin];
str[begin] = temp;
result += permutation(str,begin+1);
temp = str[i];
str[i] = str[begin];
str[begin] = temp;
}
return result;
}

 

题目5.字符串的组合,包括全组合和选N组合,N=(1,2,3,...,strlen(str));

假设我们想在长度为n的字符串中求m个字符的组合。我们先从头扫描字符串的第一个字符。针对第一个字符,我们有两种选择:第一是把这个字符放到组合中去,接下来我们需要在剩下的n-1个字符中选取m-1个字符。

#include <iostream>
#include <string>
#include <vector>
#include <math.h>

using namespace std;

string strInput("abcd");

void main()
{
int TableSize = 1;
for (int i = 0; i < strInput.length(); ++i)
{
TableSize *= 2;
}
//初始化字符串表,长度2^length
vector<string> stringTable(TableSize);
stringTable[0] = "";
//currentBit用于标识现在处理的哪一位,currentNum用于标识应减去的最高位值,用于对子字符串的索引
int currentBit = 0;
int currentNum = 1;
int inputLength = strInput.length();
for (int i = 1; i < TableSize; ++i)
{
if (i == currentNum * 2)
{
++currentBit;
currentNum *= 2;
}
//索引子字符串
int substrIndex = i - currentNum;
stringTable[i] = strInput.substr(inputLength - currentBit - 1, 1) + stringTable[substrIndex];
}
}

转自:http://blog.csdn.net/zz198808/article/details/7657168

 

对 于每个字母都有两种选择,选他或者不选他,我们可以用一个二进制位表示选择情况,1表示选择,0表示不选择,如ab = 110, c = 001, abc = 111。而且abc我们可以通过ab和c这两个字符串相加得到。所以我们可以通过自底向上的方法,缓存中间结果,省去不必要的重复计算。

 

#include <iostream>
#include <string>
#include <vector>
#include <math.h>

using namespace std;

string strInput("abcd");

void main()
{
int TableSize = 1;
for (int i = 0; i < strInput.length(); ++i)
{
TableSize *= 2;
}
//初始化字符串表,长度2^length
vector<string> stringTable(TableSize);
stringTable[0] = "";
//currentBit用于标识现在处
1c13f
理的哪一位,currentNum用于标识应减去的最高位值,用于对子字符串的索引
int currentBit = 0;
int currentNum = 1;
int inputLength = strInput.length();
for (int i = 1; i < TableSize; ++i)
{
if (i == currentNum * 2)
{
++currentBit;
currentNum *= 2;
}
//索引子字符串
int substrIndex = i - currentNum;
stringTable[i] = strInput.substr(inputLength - currentBit - 1, 1) + stringTable[substrIndex];
}
}

 

转自:http://blog.csdn.net/spritexyb/article/details/6969687

 

问题6.包含最短的字符串.S = “acbbacaT = “aba“ 求在S中一个最短包含T中元素的串。输出应为baca

解法:

#include <stdlib.h>
#include <string.h>
#include <malloc.h>

char* min_include(char* s,char* t){
int a[256];
int i;
memset(a,0,sizeof(a));
int p_begin=0;//工作指针
int p_end=0;
int begin=0;//保存结果
int end=strlen(s)-1;
while(p_end<strlen(s)){
a[(int)s[p_end]]++;
while(a[(int)s[p_begin]]>1){
a[(int)s[p_begin]]--;
p_begin++;
if(p_end-p_begin<end-begin){
begin = p_begin;
end = p_end;
}
}
p_end++;
}
char* result = malloc(sizeof(char)*(end-begin+2));
memccpy((void*)result,(void*)s+begin,'\0',end-begin+1);
result[end-begin+1] = '\0';
return result;
}

int main(){
printf("最小包含:%s\r\n",min_include("abbabaa","aba"));
return 0;
}

 

 

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: