为列表中所有字符串组合的可能输出生成逻辑

如果列表中存在代码,我正在尝试将响应生成为真或假。因此,如果字符串包含“单个括号内”值,例如:“ABC(Q,E,1)EEE”,我能够生成响应,但如果字符串具有多个括号,例如:“B(A,1 )AA(E,Z)EE",我无法从中生成输出。我是编码和构建逻辑的新手,如果有人可以提供帮助,那就太好了。


public static void main(String[] args) {


        Scanner scan = new Scanner(System.in);

        System.out.println("Enter the code you want to check: ");

        String input = scan.next();


        List<String> codes = new ArrayList<>();

        codes.add("ABC(Q,E,1)EEE");

        codes.add("ABDCE(E,Z,X)E");

        codes.add("B(A,1)AAEEE");

        codes.add("R(1,2,3,4,5)RT(U,M,N,B,V,H)(Q,E,R,F,G,H)(R,Z)");

        codes.add("B(A,1)AA(E,Z)EE");


        for (Iterator<String> i = codes.iterator(); i.hasNext(); ) {

            String code = i.next();


            String prefix = code.substring(0, code.indexOf("("));

            String suffix = code.substring(code.indexOf(")") + 1);

            String middle = code.substring(code.indexOf("(") + 1, code.indexOf(")"));

            String[] var = middle.split(",");


            String[] result = new String[var.length];

            for (int j = 0; j < var.length; j++) {


                result[j] = prefix + var[j] + suffix;


                if (result[j].equals(input)) {

                    System.out.println("True: This code is present");

                }

               }

              }

              } 

输出(有效):


Enter the code you want to check: 

BAAAEEE                

True: The code is present

输出(不工作):


Enter the code you want to check: 

BAAAZEE

<gives no output>

让我给你一个例子(对于“ABC(Q,E,1)EEE”)正在做的事情:它产生这个字符串的三个可能的输出,它们是:“ABCQEEE”、“ABCEEEE”、“ABC1EEE”。因此,如果我将输入指定为 "ABCQEEE" ,它将在内部生成这些输出,如果代码出现在列表中的任何位置,则输出为 True。



繁华开满天机
浏览 122回答 2
2回答

慕勒3428872

如果您所要做的就是根据用户输入输出真或假,您可以将代码字符串转换为正则表达式并检查输入是否与正则表达式列表匹配。脚步:将代码列表中的每个元素转换为正则表达式// 将 "ABC(Q,E,1)EEE" 转换为 "ABC[QE1]EEE" 以匹配以 ABC 开头、后跟 [QE1] 之一并以 EEE 结尾的每个字符串//"R(1,2,3,4,5)RT(U,M,N,B,V,H)(Q,E,R,F,G,H)(R,Z)" 到 " R[12345]RT[UMNBVH][QERFGH][RZ]"ETC检查输入是否匹配正则表达式之一例子:public static void main(String[] args) {&nbsp; &nbsp; Scanner scan = new Scanner(System.in);&nbsp; &nbsp; System.out.println("Enter the code you want to check: ");&nbsp; &nbsp; String input = scan.next();&nbsp; &nbsp; List<String> codes = new ArrayList<>();&nbsp; &nbsp; codes.add("ABC(Q,E,1)EEE");&nbsp; &nbsp; codes.add("ABDCE(E,Z,X)E");&nbsp; &nbsp; codes.add("B(A,1)AAEEE");&nbsp; &nbsp; codes.add("R(1,2,3,4,5)RT(U,M,N,B,V,H)(Q,E,R,F,G,H)(R,Z)");&nbsp; &nbsp; codes.add("B(A,1)AA(E,Z)EE");&nbsp; &nbsp; //list to store the modified strings&nbsp; &nbsp; List<String> modifiedCodes = new ArrayList<>();&nbsp; &nbsp; //for each string in list find if there is a pattern like '('some chars')'&nbsp; &nbsp; Pattern p = Pattern.compile("\\(.*\\)");&nbsp; &nbsp; for (Iterator<String> i = codes.iterator(); i.hasNext();) {&nbsp; &nbsp; &nbsp; &nbsp; String code = i.next();&nbsp; &nbsp; &nbsp; &nbsp; StringBuffer&nbsp; sb = new StringBuffer ();&nbsp; &nbsp; &nbsp; &nbsp; Matcher m = p.matcher(code);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while (m.find()) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String match = m.group();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //if found a match replace '(' and ')' with '[' and ']' and remove commas&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m.appendReplacement(sb, match.replace('(', '[').replace(')', ']').replace(",", ""));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m.appendTail(sb);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //add modified string to list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modifiedCodes.add(sb.toString());&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; boolean codeIsPresent = false;&nbsp; &nbsp; for(String code: modifiedCodes){&nbsp; &nbsp; &nbsp; &nbsp; //check if input matches one of the regex in the list 'modifiedCodes'&nbsp; &nbsp; &nbsp; &nbsp; if (input.matches(code)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; codeIsPresent = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("True: This code is present");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if(!codeIsPresent){&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Code not found");&nbsp; &nbsp; }}编辑我们如何打印从中获取输出的字符串的所有组合的列表?比如说,我只有一个字符串“BA(1,2,3)QW(AZ,0-9)”,我想要它的所有可能组合您评论中的上述问题与原始帖子略有不同,如果您发布一个新问题可能会更好。您可以使用某种树形结构创建自己的算法来解决问题,但它可能非常笨拙和混乱。如果可能的话,我建议使用像generex这样的第3 方图书馆。你可以从这里的 maven repo下载 jar 。使用generex,您可以拥有所有可能的组合:public static void main(String args[]){&nbsp; &nbsp; //change your input to a regular expression&nbsp; &nbsp; //"BA(1,2,3)QW(A-Z,0-9)"&nbsp; to&nbsp; "BA[1-3]QW[A-Z][0-9]"&nbsp; &nbsp; Generex generex = new Generex("BA[1-3]QW[A-Z][0-9]");&nbsp; &nbsp; List<String> matchedStrs = generex.getAllMatchedStrings();&nbsp; &nbsp; matchedStrs.forEach(System.out::println);}&nbsp;

杨__羊羊

尝试这个。 已编辑:添加了代码注释。import java.util.ArrayList;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Scanner;public class Main {&nbsp; &nbsp; public static void main(String args[]) {&nbsp; &nbsp; &nbsp; &nbsp; Scanner scan = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter the code you want to check: ");&nbsp; &nbsp; &nbsp; &nbsp; String input = scan.next();&nbsp; &nbsp; &nbsp; &nbsp; scan.close();&nbsp; &nbsp; &nbsp; &nbsp; List<String> codes = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; codes.add("ABC(Q,E,1)EEE");&nbsp; &nbsp; &nbsp; &nbsp; codes.add("ABDCE(E,Z,X)E");&nbsp; &nbsp; &nbsp; &nbsp; codes.add("B(A,1)AAEEE");&nbsp; &nbsp; &nbsp; &nbsp; codes.add("R(1,2,3,4,5)RT(U,M,N,B,V,H)(Q,E,R,F,G,H)(R,Z)");&nbsp; &nbsp; &nbsp; &nbsp; codes.add("B(A,1)AA(E,Z)EE");&nbsp; &nbsp; &nbsp; &nbsp; for (Iterator<String> i = codes.iterator(); i.hasNext();) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String code = i.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<String> codePossiblity = generatePossibilities(code);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // check if the input is in the list of all the possibility&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (String s : codePossiblity) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (s.contains(input)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("True: This code is present");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; /* This method removes the parenthesis and generates all the possibilities.&nbsp; &nbsp; &nbsp;* This method assumes that the parenthesis always comes in pair, thus&nbsp; &nbsp; &nbsp;* for every opening parenthesis ["("] there is a closing parenthesis [")"]&nbsp; &nbsp; &nbsp;* Example if the "code" is [A(WX)C(YZ)] then it will generate AWCY, AWCZ, AXCY and AXCZ&nbsp;&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @param code - The string which contains parenthesis.&nbsp; &nbsp; &nbsp;* @return a list of all the possibilities&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public static List<String> generatePossibilities(String code) {&nbsp; &nbsp; &nbsp; &nbsp; // This will hold the left part of the possibleCodes (part before "(")&nbsp; &nbsp; &nbsp; &nbsp; List<String> possibleCodeList = new LinkedList<>();&nbsp; &nbsp; &nbsp; &nbsp; String s = code;&nbsp; &nbsp; &nbsp; &nbsp; boolean first = true;&nbsp; &nbsp; &nbsp; &nbsp; // Loop while an open parenthesis ["("] can be found&nbsp; &nbsp; &nbsp; &nbsp; while (s.contains("(")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Retrieve from the string the first substring where "(" starts and ends with ")"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // In the example, in the first iteration will be "WX"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // in the second iteration this will be "YZ"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String inside = s.substring(s.indexOf("(") + 1, s.indexOf(")"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Retrieve the list inside the "(" and ")"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // In the example, in the first iteration the list will have "W", "X"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // in the second iteration the list will have "Y", "Z"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] listOfChoices = inside.split(",");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // This will hold the right part of the possibleCodes (part after ")")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<String> listOfCombinations = new LinkedList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Loop all the possible choices&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (String choice : listOfChoices) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // If it is the first iteration then you need to include those characters before the "("&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (first) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // add the characters before the "(" and the remaining characters after ")"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // In the example, the first iteration of this list ("W", "X") will add "AWC(YZ)"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // the second iteration of this list ("W", "X") will add "AXC(YZ)"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listOfCombinations.add(s.substring(0, s.indexOf("(")) + choice + s.substring(s.indexOf(")") + 1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Else just start with choice&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // add the remaining characters after ")"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // In the example, the first iteration of this list ("Y", "Z") will add "Y"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // the second iteration of this list ("Y", "Z") will add "Z"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listOfCombinations.add(choice + s.substring(s.indexOf(")") + 1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Remove the subtring before the ")", in the example this will be "C(YZ)"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s = s.substring(s.indexOf(")") + 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // If it is the first iteration then you just need to assign the listOfCombinations directly to possibleCodeList,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // since possibleCodeList is still empty&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (first) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; possibleCodeList = listOfCombinations;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; first = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Else combine the left and right part&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<String> codePossiblity2 = new LinkedList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Iterate though all the list of possible codes since we want all the elements in the list to be concatenated with the right half of the string&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // The list will have "AWC(YZ)" and "AXC(YZ)"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (String possibleCodes : possibleCodeList) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Iterate the possible combinations of the right half of the original string (the second pair of "()")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // The list will have "Y" and "Z"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (String sTmp : listOfCombinations) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Replace the string which are inside the "()" in the left half of the original string.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Replace it with the right half of the original string&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // In the string of "AWC(YZ)" replace "(YZ)" with "Y"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // In the string of "AWC(YZ)" replace "(YZ)" with "Z"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // In the string of "AXC(YZ)" replace "(YZ)" with "Y"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // In the string of "AXC(YZ)" replace "(YZ)" with "Z"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String t = possibleCodes.replace("(" + inside + ")", sTmp);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // add the newly created string to codePossiblity2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; codePossiblity2.add(t);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // At the end of the loop above codePossiblity2 will have these values&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // AWCY, AWCZ, AXCY and AXCZ&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // overwrite the possibleCodeList since we have now a new left part of the string&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; possibleCodeList = codePossiblity2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return possibleCodeList;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java