1. 第一题:
import java.util.*;
public class Main1 {
public static final String REG_NUMBER = ".*\\d+.*";
public static final String REG_UPPERCASE = ".*[A-Z]+.*";
public static final String REG_LOWERCASE = ".*[a-z]+.*";
public static final String REG_SYMBOL = ".*[~!@#$%^&*()_+|<>,.?/:;'\\[\\]{}\"]+.*";
public static int check1(String password)
{
if(password == null || password.length() < 8 || password.length() > 120)
{
return 1;
}
int count = 0;
if(password.matches(REG_NUMBER)) count++;
if(password.matches(REG_UPPERCASE)) count++;
if(password.matches(REG_LOWERCASE)) count++;
if(password.matches(REG_SYMBOL)) count++;
if(count != 4)
{
return 2;
}
return 0;
}
public static int check2(String password) {
int n = password.length();
if (n < 8 || n > 120) return 1;
boolean num = false, symbol = false, D = false, X = false;
for (int i = 0; i < n; ++i) {
if (num == true && symbol == true && D == true && X == true) return 0;
if (password.charAt(i) >= '0' && password.charAt(i) <= '9') num = true;
if (password.charAt(i) >= 'a' && password.charAt(i) <= 'z') X = true;
if (password.charAt(i) >= 'A' && password.charAt(i) <= 'Z') D = true;
else symbol = true;
}
return 2;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext())
{
String str = sc.next();
int res1 = check1(str);
System.out.println(res1);
int res2 = check2(str);
System.out.println(res2);
}
}
}
2. 第二题:
import java.util.*;
public class Main2 {
public static boolean exist(char[][] board, String word)
{
if(board == null || board.length ==0 || board[0].length == 0 || word.equals(""))
{
return false;
}
int m = board.length;
int n = board[0].length;
boolean visited[][] = new boolean[m][n];
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
if(board[i][j] == word.charAt(0) && dfs(i, j, 0, word, visited, board))
{
return true;
}
}
}
return false;
}
public static boolean dfs(int i, int j, int idx, String word, boolean visited[][], char board[][])
{
if(idx == word.length())
{
return true;
}
if(i < 0 || i >= board.length || j < 0 || j >= board[0].length || board[i][j] != word.charAt(idx) || visited[i][j])
{
return false;
}
visited[i][j] = true;
if(dfs(i + 1, j, idx + 1, word, visited, board) || dfs(i, j + 1, idx + 1, word, visited, board)
|| dfs(i - 1, j, idx + 1, word, visited, board) || dfs(i, j - 1, idx + 1, word, visited, board))
{
return true;
}
visited[i][j] = false;
return false;
}
public static void main(String[] args) {
char board[][] = {{'A','B','C','E'}, {'S','F','C','S'}, {'A','D','E','E'}};
Scanner sc = new Scanner(System.in);
String word = sc.nextLine();
boolean res = exist(board, word);
System.out.println(res);
}
}
参考:
- Java密码强度正则表达式 – 必须包含大小写字母、数字、符号满足其中的3种