由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 看一道string permutation的题目吧
相关主题
一个容易记忆的permutation算法有重复元素的全排列,递归算法
Exposed上一道string permutation的题面经
Non-recursive permutationFB phone interview
问一个题新鲜Amazon面经
问一个题目生成一个有重复数的全排列,怎么做比较好
用了递归以后,怎么计算空间复杂度?用 c 实现的字符串 permutation,求批评指点
Permutation leetcode-关于permutation和combination
攒rp,发个L家面经晕了,有人用iteration解n queens么
相关话题的讨论汇总
话题: int话题: cout话题: i2话题: i1话题: i6
进入JobHunting版参与讨论
1 (共1页)
H***e
发帖数: 476
1
string = '"abc",
打印所有长度为6的字符可以重复的string permutation(但是输出不能重复)
也就是打印
aaaaaa
aaaaab
aaaaac
aaabac
....
我觉得很费解。
c**********e
发帖数: 2007
2
This looks nasty, but it should work.
char* s=string.c_str();
for(int i1=0;i1<3;i1++) {
for(int i2=0;i2<3;i2++) {
for(int i3=0;i3<3;i3++) {
for(int i4=0;i4<3;i4++) {
for(int i5=0;i5<3;i5++) {
for(int i6=0;i6<3;i6++) {
cout << s[i1] << s[i2] << s[i3] << s[i4] << s[i5] << s[i6] << endl;
}
}
}
}
}
}
H***e
发帖数: 476
3
i guess it is asking for a systematic method...

【在 c**********e 的大作中提到】
: This looks nasty, but it should work.
: char* s=string.c_str();
: for(int i1=0;i1<3;i1++) {
: for(int i2=0;i2<3;i2++) {
: for(int i3=0;i3<3;i3++) {
: for(int i4=0;i4<3;i4++) {
: for(int i5=0;i5<3;i5++) {
: for(int i6=0;i6<3;i6++) {
: cout << s[i1] << s[i2] << s[i3] << s[i4] << s[i5] << s[i6] << endl;
: }

c**********e
发帖数: 2007
4
First, create a c-str which has no replicates. The length is N. If we want
to get new strings with length M, it is a simple replication. It can be done
recursively for M. The total number of rows is N^M.

【在 H***e 的大作中提到】
: i guess it is asking for a systematic method...
s******n
发帖数: 226
5
Don't need so many for loops. Only 2 while loop will work.
Go to see 编程之美
H***e
发帖数: 476
6
谢谢
全称叫什么名字呢

【在 s******n 的大作中提到】
: Don't need so many for loops. Only 2 while loop will work.
: Go to see 编程之美

c**********e
发帖数: 2007
7
Use two loops:
for(int i=0;i<729;i++) {
for(int j=0;j<6;j++) {
m = k % 3; k = k/3;
cout << s[m];
}
cout << end;
}

【在 s******n 的大作中提到】
: Don't need so many for loops. Only 2 while loop will work.
: Go to see 编程之美

c**********e
发帖数: 2007
8
Use one loop: (Hehe, nasty again)
for(int i=0;i<3^6;i++) {
i1 = i/243;
i2 = (i/81) % 3;
i3 = (i/27) % 3;
i4 = (i/9) % 3;
i5 = (i/3) % 3;
i6 = i % 3;
cout << s[i1] << s[i2] << s[i3] << s[i4] << s[i5] << s[i6] << endl;
}
c**********e
发帖数: 2007
9
This one can be generalized: To permut s[N], use two loops
for(int i=0;i for(int j=0;j m = k % N; k = k/N;
cout << s[m];
}
cout << end;
}
s******n
发帖数: 226
10
OK. I did not test your solution, but the solution offerred by MSRA still
seems clearer than yours.

【在 c**********e 的大作中提到】
: This one can be generalized: To permut s[N], use two loops
: for(int i=0;i: for(int j=0;j: m = k % N; k = k/N;
: cout << s[m];
: }
: cout << end;
: }

c**********e
发帖数: 2007
11
Why not post the MSRA solution here?

【在 s******n 的大作中提到】
: OK. I did not test your solution, but the solution offerred by MSRA still
: seems clearer than yours.

s******n
发帖数: 226
12
I am packing for moving, so I cannot find it. Just try from my rememberance
idea: same as phone pad interprete to word:
char phonePad[m][n]; // phone pad
int answer[t]; // index of char in the answers corresponding yo each
digits
int num[t]; // phone number to interprete
int k = t-1;
while(true){
print; // using phonePad
while(k>=0){
if( answer[k] answer[k]++;
break;
}
answer[k] = 0;
k--;
}
if(k<0) break;
}

【在 c**********e 的大作中提到】
: Why not post the MSRA solution here?
q****x
发帖数: 7404
13
就是abc三个字符可重复,输出所有组合。每个位置都有三个选择,相当于六位的三进
制数穷举。

【在 H***e 的大作中提到】
: string = '"abc",
: 打印所有长度为6的字符可以重复的string permutation(但是输出不能重复)
: 也就是打印
: aaaaaa
: aaaaab
: aaaaac
: aaabac
: ....
: 我觉得很费解。

1 (共1页)
进入JobHunting版参与讨论
相关主题
晕了,有人用iteration解n queens么问一个题目
请教ebay 的面试题一道用了递归以后,怎么计算空间复杂度?
C++: what is the output? How to interpret it?Permutation leetcode-
新手请教:C++ decrement loop (转载)攒rp,发个L家面经
一个容易记忆的permutation算法有重复元素的全排列,递归算法
Exposed上一道string permutation的题面经
Non-recursive permutationFB phone interview
问一个题新鲜Amazon面经
相关话题的讨论汇总
话题: int话题: cout话题: i2话题: i1话题: i6