Algorithm training JAM counting method java problem solving

Resource constraints

Time limit: 1.0s   Memory limit: 256.0MB

Problem description

Jam is a science freak who likes to be unconventional. Instead of counting with Arabic numerals, he used lowercase English letters. He felt that doing so would make the world more colorful. In his counting method, the number of digits of each number is the same (using the same number of letters). The English letters are in the original order, and the letters in front of them are smaller than those after them. We call such "numbers" jam numbers. In jam numbers, each letter is different from each other and increases strictly from left to right. Each time, jam also specifies the range of letters to use, for example, from 2 to 10, which means that only {b,c,d,e,f,g,h,i,j} can be used. If the number of digits is specified as 5, the number immediately after jam number "bdfij" should be "bdghi". (if we use U and V to represent jam numbers "bdfij" and "bdghi" in turn, then U < V < span >, and there is no jam number P, so that U < p < V < span >). Your task is: for a jam number read from the file, output the next five jam numbers in sequence. If there are not so many jam numbers, output several.

Input format

There are 2 lines, the first line is 3 positive integers, separated by a space:
  s t w
(where s is the serial number of the smallest letter used, t is the serial number of the largest letter used. W is the number of digits, and these three numbers meet: 1 ≤ s < T ≤ 26, 2 ≤ w ≤ t-s)
The second line is a string with w lowercase letters, which is a qualified Jam number.
The data given are correct and do not need to be verified.

Output format

Up to 5 lines, which are the 5 Jam numbers immediately after the input Jam numbers. If there are not so many Jam numbers, several will be output. Only one Jam number is output on each line, which is a string composed of w lowercase letters without redundant spaces.

sample input

2 10 5
bdfij

sample output

bdghi
bdghj
bdgij
bdhij
befgh

Problem solving ideas:

Select the five digits that increase the dictionary order in turn from the 26 letters, and require the output of the next five sequences. In fact, the restriction condition is added on the basis of full arrangement, that is, the dictionary order needs to be increased, which can reduce the depth of recursion. Just judge whether the dictionary order is larger than the previous letter before adding letters each time. After the starting sequence of jam is given in the recursive finding problem, five sequences can be output in turn.

Full permutation problem:

  Total permutation problemhttps://blog.csdn.net/weixin_48898946/article/details/121589155

java code:

import java.io.*;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] split = br.readLine().split(" ");
		int s = Integer.parseInt(split[0]);
		int e = Integer.parseInt(split[1]);
		int len = Integer.parseInt(split[2]);
		String str = br.readLine();
		Temp32 obj = new Temp32(s, e, len, str);
		obj.dfs(1);
	}
}
class Temp32{
	int s;//Start subscript
	int e;//End subscript
	int len;//Digital length
	String str;//The jam start sequence given in question
	int count = 0;//Counter to control the number of outputs
	boolean flag = false;//Flag jam to start the next item in the sequence
	char []ch = new char[27];//Store 26 lowercase letters, starting from 1 in the following table
	boolean []vis = new boolean[27];//Indicates whether a letter has appeared
	StringBuilder builder = new StringBuilder();//Real time splicing
	
	public Temp32(int s, int e, int len, String str) {
		this.s = s;
		this.e = e;
		this.len = len;
		this.str = str;
		for(int i = 1; i <= 26; i++) {
			ch[i] = (char)(96 + i);
		}
	}
	
	public void dfs(int step) {//The letter to be placed in the step length
		if(step > len) {//It's full
			if(count == 0) {//Find the jam start sequence given by the question
				flag = str.equals(builder.toString());//Found flag is true
			}
			if(flag) {//Output 5 times
				count++;
				if(count <= 6 && count != 1) {
					System.out.println(builder);
				}
				if(count > 6) {
					System.exit(0);
				}
			}
			return;
		}
		for(int i = s; i <= e; i++) {//From s pendulum to e
			if(!vis[i]) {//Not visited
				if(step > 1 && ch[i] <= builder.charAt(step - 2)) {
					continue;//It is necessary to ensure that the last letter is larger than the previous dictionary
				}
				vis[i] = true;//Visited
				builder.append(ch[i]);//Add to builder
				dfs(step + 1);//Place next letter
				vis[i] = false;//to flash back
				builder.deleteCharAt(builder.length() - 1);
			}
		}
	}
}

Submit screenshot:

Tags: Java search engine dfs

Posted on Fri, 03 Dec 2021 21:38:55 -0500 by freshrod