从输入的字符数组查找所有可能的单词(排列)

编程入门 行业动态 更新时间:2024-10-26 20:24:01
本文介绍了从输入的字符数组查找所有可能的单词(排列)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

经过几个职位我仍然排列和递归函数难倒看完。我试图创建人物的所有可能的3个字母的排列在长度不等的二维数组,其中第一个字母是从集合{'L','M','N'},第二个字母是从集合{'Q ','R'}第三个字母是从集合{'A','E','我','O'}。我的code经过正确的置换模式,但不会打印出正确的输出。例如,如果第一8置换被认为是:

LQALQELQILQOLRALRELRILRO

我的code打印出:

LQAË一世ØRAË一世Ø

这是什么问题任何想法?这里是我的code的相关部分:

REC(character_pools,0,3);无效REC(字符**池,诠释K,诠释J){    如果(K == j)条    {        的printf(\\ n);        返回;    }    INT I,LEN = strlen的(池[K]);    对于(i = 0; I< LEN,我++)    {        的printf(%C,池[k]的[I]);        REC(池,K + 1,J);    }}

解决方案

您创建一个字符数组将包含您要使用字符串字符海峡[] =ABC;

那么你得到的字符串的长度 INT N = strlen的(STR); ,最后你排列替换

您使一个新的功能,这将包含输入字符串,开始字符串的索引和结束字符串的索引。检查起始索引( int类型)等于结束索引( INTè)如果确实如此,这意味着大功告成,如果不是你进入一个循环,从启动(S)结束(E)去,再交换价值,递归,交换原路返回。

在C ++中的一个例子:

的#include<&stdio.h中GT;#包括LT&;&string.h中GT;无效掉期(字符*我,字符* j)条{    焦温度;    TEMP = *我;    * I = *焦耳;    * J =温度;}无效排列替换(的char * str中,诠释开始,诠释完){    INT I;    如果(开始==结束)        的printf(%S \\ n,STR);    其他    {        对于(i =启动; I< =结束;我++)        {            掉期((STR +开始),(STR + I));            排列替换(STR,启动+ 1,结束);            掉期((STR +开始),(STR + I)); //原路返回        }    }}诠释的main(){    焦炭海峡[] =ABC;    INT N = strlen的(STR);    排列替换(STR,0,N - 1);    返回0;}

After reading through several posts I am still stumped with permutations and recursive functions. I am trying to create all possible 3 letter permutations of characters in an unequal length 2D array where the first letter is from the set {‘l’, ‘m’, ‘n’}, the second letter is from the set {‘q’, ‘r’} and the third letter is from the set {‘a’, ‘e’, ‘i’, ‘o’}. My code goes through the correct permutation pattern but does not print out the correct output. For example, if the first 8 permutations are supposed to be:

lqa lqe lqi lqo lra lre lri lro

my code prints out:

lqa e i o ra e i o

Any ideas on what the problem is? Here are the relevant pieces of my code:

rec(character_pools,0,3); void rec(char** pool, int k, int j) { if(k==j) { printf("\n"); return; } int i,len=strlen(pool[k]); for (i=0;i<len;i++) { printf("%c",pool[k][i]); rec(pool,k+1,j); } }

解决方案

You create a char array which will contain the string you want to work with char str[] = "ABC";

then you get the length of the string int n = strlen(str); and lastly you permutate.

You make a new function which will contain the input string, starting index of the string and ending index of the string. Check if the starting index (int s) equals the ending index (int e) if it does, that means you're done, if not you go into a loop where you go from start (s) to end (e), swap the values, recurse, swap again to backtrack.

An example in C++:

#include <stdio.h> #include <string.h> void swap(char *i, char *j) { char temp; temp = *i; *i = *j; *j = temp; } void permutate(char *str, int start, int end) { int i; if (start == end) printf("%s\n", str); else { for (i = start; i <= end; i++) { swap((str + start), (str + i)); permutate(str, start + 1, end); swap((str + start), (str + i)); //backtrack } } } int main() { char str[] = "ABC"; int n = strlen(str); permutate(str, 0, n - 1); return 0; }

更多推荐

从输入的字符数组查找所有可能的单词(排列)

本文发布于:2023-11-30 10:07:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649603.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   单词   排列   字符

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!