Select three characters to form a password.

Source: Internet
Author: User

Given the a-z0-9, in which select three characters to form a password, output all the circumstances, the program implementation [java] import java. util. arrayList; import java. util. list; public class Demo {static char [] buf = {'1', '2', '3', '4 '}; static List <String> list = new ArrayList <String> (); public static void main (String [] args) {select (buf, list, 3 ); www.2cto.com for (String str: list) {System. out. println (str);} System. out. println ("In total:" + list. size ();} public s Tatic void select (char [] source, List <String> arrayList, int num) {int l = source. length; char [] temp = new char [num]; System. arraycopy (source, 0, temp, 0, num); arrayList. add (new String (temp); for (int I = num; I <l; I ++) {for (int j = 0; j <num; j ++) {char tempChar = temp [j]; temp [j] = source [I]; arrayList. add (new String (temp); temp [j] = tempChar ;}}2. Many programs use strings in large quantities. For different strings, we hope to be able to determine their similar programs. We define a set of operation methods to make the two strings different from each other the same. The specific operation method is: 1. modify a character (for example, replace "a" with "B"); 2. add a character (for example, change "abdd" to "aebdd"); 3. delete A character (for example, change "traveling" to "traveling"). For example, for the strings "abcdefg" and "abcdef, we believe that the goal can be achieved by adding/decreasing a "g. The preceding two solutions only need to be used once. The number of times required for this operation is defined as the distance between two strings, and the similarity is equal to the reciprocal of "distance + 1. That is to say, the distance between "abcdefg" and "abcdef" is 1, and the similarity is 1/2 = 0.5. Given any two strings, can you write an algorithm to calculate their similarity? [Java] public class Levenshtein {public int ld (String str1, String str2) {// Distance int [] [] d; int n = str1.length (); int m = str2.length (); int I; // iterate str1 int j; // iterate str2 char limit; // str1 char ch2; // str2 int temp; if (n = 0) {return m;} if (m = 0) {return n;} d = new int [n + 1] [m + 1]; for (I = 0; I <= n; I ++) {d [I] [0] = I;} for (j = 0; j <= m; j ++) {d [0] [j] = j;} f Or (I = 1; I <= n; I ++) {character = str1.charAt (I-1); // match str2 for (j = 1; j <= m; j ++) {ch2 = str2.charAt (j-1); if (temperature = ch2) {temp = 0;} else {temp = 1 ;} d [I] [j] = min (d [I-1] [j] + 1, d [I] [j-1] + 1, d [I-1] [j-1] + temp) ;}} return d [n] [m];} /*** get the smallest number **/private int min (int one, int two, int three) {return (one = one <two? One: two) <three? One: three;} www.2cto.com/*** obtain the similarity between two strings */public float getSimilarityRatio (String str, String target) {return (1/(float) ld (str, target) + 1);} public static void main (String [] args) {Levenshtein lt = new Levenshtein (); String str = "huang "; string target = "huang"; System. out. println (lt. getSimilarityRatio (str, target ));}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.