(mã dưới đây liên quan đến câu hỏi của tôi)hoán vị/Tham khảo trong Objective-C - tôi thiếu cái gì
mỗi this stack overflow question tôi đã sử dụng cách tiếp cận Pegolon để tạo ra tất cả các hoán vị có thể của một nhóm các nhân vật bên trong một NSString. Tuy nhiên, bây giờ tôi đang cố gắng làm cho nó không chỉ tạo ra một ANAGRAM mà là tất cả hoán vị có cùng độ dài, nhưng tất cả các kết hợp có thể (bất kỳ độ dài nào) của các ký tự trong một chuỗi.
Có ai biết làm thế nào tôi sẽ thay đổi mã sau đây để làm cho nó để làm điều này? Điều này giống như: Generate All Permutations of All Lengths - nhưng (vì sợ họ cần câu trả lời cho bài tập về nhà) họ không để lại mã. Tôi có một mẫu của những gì tôi nghĩ sẽ làm điều đó ở dưới cùng của bài đăng này ... nhưng nó không.
Vì vậy, mã này, như là, tạo ra the
, teh
, hte
, het
, eth
và eht
khi trao THE
. Những gì tôi cần là dọc theo các dòng: t
, h
, e
, th
, ht
, te
, he
(v.v) ngoài các kết hợp 3 ký tự ở trên.
Làm cách nào để thay đổi điều này, vui lòng. (ps: Có hai phương pháp trong điều này. Tôi đã thêm allPermutationsArrayofStrings
để nhận kết quả trở lại dưới dạng chuỗi, như tôi muốn chúng, không chỉ là một mảng các ký tự trong mảng khác). Tôi giả sử sự kỳ diệu sẽ xảy ra trong pc_next_permutation
anyway - nhưng nghĩ rằng tôi sẽ đề cập đến nó.
Trong NSArray + Permutation.h
#import <Foundation/Foundation.h>
@interface NSArray(Permutation)
- (NSArray *)allPermutationsArrayofArrays;
- (NSArray *)allPermutationsArrayofStrings;
@end
trong NSArray + Permutation.m:
#define MAX_PERMUTATION_COUNT 20000
NSInteger *pc_next_permutation(NSInteger *perm, const NSInteger size);
NSInteger *pc_next_permutation(NSInteger *perm, const NSInteger size)
{
// slide down the array looking for where we're smaller than the next guy
NSInteger pos1;
for (pos1 = size - 1; perm[pos1] >= perm[pos1 + 1] && pos1 > -1; --pos1);
// if this doesn't occur, we've finished our permutations
// the array is reversed: (1, 2, 3, 4) => (4, 3, 2, 1)
if (pos1 == -1)
return NULL;
assert(pos1 >= 0 && pos1 <= size);
NSInteger pos2;
// slide down the array looking for a bigger number than what we found before
for (pos2 = size; perm[pos2] <= perm[pos1] && pos2 > 0; --pos2);
assert(pos2 >= 0 && pos2 <= size);
// swap them
NSInteger tmp = perm[pos1]; perm[pos1] = perm[pos2]; perm[pos2] = tmp;
// now reverse the elements in between by swapping the ends
for (++pos1, pos2 = size; pos1 < pos2; ++pos1, --pos2) {
assert(pos1 >= 0 && pos1 <= size);
assert(pos2 >= 0 && pos2 <= size);
tmp = perm[pos1]; perm[pos1] = perm[pos2]; perm[pos2] = tmp;
}
return perm;
}
@implementation NSArray(Permutation)
- (NSArray *)allPermutationsArrayofArrays
{
NSInteger size = [self count];
NSInteger *perm = malloc(size * sizeof(NSInteger));
for (NSInteger idx = 0; idx < size; ++idx)
perm[idx] = idx;
NSInteger permutationCount = 0;
--size;
NSMutableArray *perms = [NSMutableArray array];
do {
NSMutableArray *newPerm = [NSMutableArray array];
for (NSInteger i = 0; i <= size; ++i)
[newPerm addObject:[self objectAtIndex:perm[i]]];
[perms addObject:newPerm];
} while ((perm = pc_next_permutation(perm, size)) && ++permutationCount < MAX_PERMUTATION_COUNT);
free(perm);
return perms;
}
- (NSArray *)allPermutationsArrayofStrings
{
NSInteger size = [self count];
NSInteger *perm = malloc(size * sizeof(NSInteger));
for (NSInteger idx = 0; idx < size; ++idx)
perm[idx] = idx;
NSInteger permutationCount = 0;
--size;
NSMutableArray *perms = [NSMutableArray array];
do {
NSMutableString *newPerm = [[[NSMutableString alloc]initWithString:@"" ]autorelease];
for (NSInteger i = 0; i <= size; ++i)
{
[newPerm appendString:[self objectAtIndex:perm[i]]];
}
[perms addObject:newPerm];
} while ((perm = pc_next_permutation(perm, size)) && ++permutationCount < MAX_PERMUTATION_COUNT);
free(perm);
return perms;
}
@end
Mã của tôi mà tôi nghĩ sẽ sửa lỗi này:
for (NSInteger i = 1; i <= theCount; i++) {
NSRange theRange2;
theRange2.location = 0;
theRange2.length = i;
NSLog(@"Location: %i (len: %i) is: '%@'",theRange2.location,theRange2.length,[array subarrayWithRange:theRange2]);
NSArray *allWordsForThisLength = [[array subarrayWithRange:theRange2] allPermutationsArrayofStrings];
for (NSMutableString *theString in allWordsForThisLength)
{
NSLog(@"Adding %@ as a possible word",theString);
[allWords addObject:theString];
}
Tôi biết nó sẽ không hiệu quả nhất .. nhưng tôi đã cố thử nghiệm.
Đây là những gì tôi nhận:
2011-07-07 14:02:19.684 TA[63623:207] Total letters in word: 3
2011-07-07 14:02:19.685 TA[63623:207] Location: 0 (len: 1) is: '(
t
)'
2011-07-07 14:02:19.685 TA[63623:207] Adding t as a possible word
2011-07-07 14:02:19.686 TA[63623:207] Location: 0 (len: 2) is: '(
t,
h
)'
2011-07-07 14:02:19.686 TA[63623:207] Adding th as a possible word
2011-07-07 14:02:19.687 TA[63623:207] Adding ht as a possible word
2011-07-07 14:02:19.688 TA[63623:207] Location: 0 (len: 3) is: '(
t,
h,
e
)'
2011-07-07 14:02:19.688 TA[63623:207] Adding the as a possible word
2011-07-07 14:02:19.689 TA[63623:207] Adding teh as a possible word
2011-07-07 14:02:19.690 TA[63623:207] Adding hte as a possible word
2011-07-07 14:02:19.691 TA[63623:207] Adding het as a possible word
2011-07-07 14:02:19.691 TA[63623:207] Adding eth as a possible word
2011-07-07 14:02:19.692 TA[63623:207] Adding eht as a possible word
Như bạn thấy, không có một hoặc hai lá thư từ - Tôi đang kéo tóc của tôi ra! (và tôi không có nhiều phụ tùng!)
Đây có phải là bài tập về nhà không? Nếu có, vui lòng gắn thẻ như vậy để chúng tôi biết cách tốt nhất để giúp bạn. –
Không, đây không phải là bài tập về nhà. Tôi gần như muốn nó được. Chỉ ra rằng tôi trẻ hơn tôi nhiều ... Tôi đang viết một chương trình cho IOS có nhu cầu thường xuyên này. – Jann
Bạn có một vài từ hoặc hai chữ cái. Tôi thấy "t" và tôi thấy "th" và "ht" – Kal