google面试题目11

/*
 * =====================================================================================
 *
 *       Filename:  g14.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2013年01月13日 16时04分48秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Weidong Yang (cn), ywdong@mail2.sysu.edu.cn
 *        Company:  
 *
 * =====================================================================================
 */
/*
Generate all the possible substrings using the characters of a given string. 
Write code. (The order of chars do not matter, i.e., ac <=> ca)
i/p: abc
o/p: { a,b,c,ab,ac,bc,abc}
分析思路:
对于每个字符我们都有两种选择,选择它或者不选择它,所以对于长度为n的字符串,总共有
2^n 种可能的substrings
缺点是有可能会出现重复的substring,例如输入是aab的时候会出现重复a,ab!
*/

#include <string>  // for c++ string class
#include <iostream>  
#include <cmath>

using namespace std;

void GenSubString(string pre, string rest)
{
    if (rest.empty())
    {
        cout << pre << endl;
        return;
    }
    GenSubString(pre+rest.at(0), rest.substr(1));
    GenSubString(pre, rest.substr(1));
    return;
}

/*
Well, think of each possible substring of the original string is a array 
of the same size of original string, if the character is included in the 
substring , the corresponding digit is 1, else is 0 for example, string "abcd" 
substring "ad" then binary form is 1001 ,print all the possible (2^n) binary 
array with function recursion, each postion in the binary array there are only 
2 possibilties.
*/

void print_all_substrings_iterative(const string& str)
{
    const size_t size = str.size();
    for (size_t i = 1; i < std::pow(2.0, (double)size); ++i) {
        for (size_t j = 0; j < size; ++j) {
            if (i & (1 << j)) {
                std::cout << str[j];
            }
        }
        std::cout << ' ';
    }
    std::cout << std::endl;
}

string Swap(string str, size_t index1, size_t index2)
{
    string res = "";
    for (size_t i = 0; i < str.size(); i++)
    {
        if (i == index1)
        {
            res.push_back(str.at(index2));
            continue;
        }
        if (i == index2)
        {
            res.push_back(str.at(index1));
            continue;
        }
        res.push_back(str.at(i));
    }
    return res;
}
/*
Printing all permutations of a string is a very common interview question. 
We'll discuss this problem and some interesting variations of it. The original 
problem of string permutation says, "print all permutations of a string". As an 
example, if the string is "abc" there are 6 permutations {abc, acb, bac, bca, cab, cba}. 
Assume that string contains no duplicate elements.

There are many ways in which this problem can be solved. The most easiest of the ways is 
to code it using simple yet powerful recursion technique.

Above code performs the swapping operation twice. First time to generate all possible 
permutations of character at that level and second time to restore to the original string. 
It is important to restore to the original string (as passed to this call), otherwise the 
algorithm ends up printing redundant permutations. Additionally, this code assumes that 
there are no duplicates in the string, else it fails. 
*/
void Permute(string str, size_t d)
{
    if (d == str.size()-1)
    {
        cout << str << endl;
        return;
    }
    for (size_t i = d; i < str.size() ; i++)
    {
        str = Swap(str, d, i);
        Permute(str, d+1);
        str = Swap(str, i, d);
    }
    return;   
}

int main ( int argc, char *argv[] )
{
    string str("abc");
    //GenSubString("", str);
    //print_all_substrings_iterative(str);
    Permute(str, 0);
	return 0;
}		// ----------  end of function main  ---------- 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值