icodingc
BAN USERUsing a bit vector.then dfs.
#include <iostream>
#include <string>
#include <vector>
#include <set>
using namespace std;
class Solution{
public:
vector<string> GenerateCase(string &str)
{
set<string>result;
if (str.length() == 0)return vector<string>();
this->selected=vector<int>(str.size(), 0);
string cur;
dfs(result, cur, 0, str);
return vector<string>(result.begin(),result.end());
}
private:
vector<int>selected;
void dfs(set<string>&result, string &cur, int start, string &str)
{
const int m = str.length();
if (start == m){
string sub;
for (int i = 0; i < m; i++)
{
if (isdigit(str[i])||!selected[i])
sub.push_back(str[i]);
else if (selected[i])
sub.push_back(str[i] ^ 32);
}
result.insert(result.begin(), sub);
return;
}
selected[start] = 0;
dfs(result, cur, start + 1, str);
selected[start] = 1;
dfs(result, cur, start + 1, str);
}
};
void print(vector<string>&result)
{
for (auto& i : result) {
cout << i << endl;
}
}
int main()
{
Solution s;
string str = "0ab";
vector<string>result = s.GenerateCase(str);
print(result);
return 0;
}
struct node{
int data;
struct node* left;
struct node* right;
};
void mirror(struct node* node){
if(node==NULL)
return;
else{
struct node* temp;
//do the subtrees
mirror(node->left);
mirror(node->right);
//swap
temp=node->left;
node->left=node->right;
node->right=temp;
}
}
- icodingc February 17, 2014I don't know how to use DP.
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
bool ispalid(string&str)
{
transform(str.begin(), str.end(), str.begin(), ::tolower);
str.erase(remove_if(str.begin(), str.end(), [](char c){return !isalnum(c); }), str.end());
return equal(str.begin(), str.begin() + str.size() / 2, str.rbegin());
}
vector<string> allPalid(string&str)
{
vector<string>result;
string temp;
for (size_t i = 0; i < str.size(); i++)
{
for (size_t j = 0; j <= i; j++)
{
temp = str.substr(j, i - j + 1);
if (ispalid(temp))
result.push_back(temp);
}
}
return result;
}
int main()
{
string str = "abcbafabcba";
vector<string>result;
result = allPalid(str);
for (auto& s : result)
{
cout << s << endl;
}
return 0;
}
- icodingc February 16, 2014
- icodingc March 28, 2014