Java字符串匹配任务求助:基于优先级的字符匹配类型判定逻辑实现
Got it, let's pivot away from that backspace code and tackle this string matching task properly. The key here is following the priority rules strictly: P first, then S, then I, and making sure we track which characters from A are still available for matches.
Core Logic Breakdown
- First pass: Mark all P matches: These are the highest priority. Every time A[i] equals B[i], we mark that position as P and decrement the count of that character in A (since it's now used).
- Second pass: Mark S matches: For each position that isn't already P, check if B[i] exists in the remaining unused characters of A. If yes, mark it as S and decrement the count again.
- Remaining positions stay I: Any spot not marked P or S is innocent.
Using a frequency array (for 26 uppercase letters) is perfect here—it's O(1) space and super fast, even for the maximum input size of 1e6 characters.
Pseudocode
// Initialize a count array for A's characters (26 for each uppercase letter) countA = array of 26 zeros // First, count all characters in string A for each character c in A: countA[c - 'A'] += 1 // Initialize result array with 'I' (default) result = array of 'I's with length N // Step 1: Process all P matches for i from 0 to N-1: if A[i] == B[i]: result[i] = 'P' countA[A[i] - 'A'] -= 1 // Mark this character as used for P // Step 2: Process all S matches for i from 0 to N-1: if result[i] == 'P': continue // Skip already matched positions bChar = B[i] charIndex = bChar - 'A' if countA[charIndex] > 0: result[i] = 'S' countA[charIndex] -= 1 // Mark this character as used for S // Convert result array to string and output print(result as string)
Java Implementation
This code is optimized for large input sizes (1e6 characters) with linear time complexity O(N) and constant space O(1):
import java.util.Scanner; public class StringMatchClassifier { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String A = scanner.nextLine().trim(); String B = scanner.nextLine().trim(); int n = A.length(); // Count frequency of each character in A int[] charCountA = new int[26]; for (char c : A.toCharArray()) { charCountA[c - 'A']++; } char[] result = new char[n]; // Step 1: Mark all P matches first for (int i = 0; i < n; i++) { char aChar = A.charAt(i); char bChar = B.charAt(i); if (aChar == bChar) { result[i] = 'P'; charCountA[aChar - 'A']--; } else { result[i] = 'I'; // Default to innocent } } // Step 2: Mark S matches for remaining positions for (int i = 0; i < n; i++) { if (result[i] == 'P') { continue; } char bChar = B.charAt(i); int idx = bChar - 'A'; if (charCountA[idx] > 0) { result[i] = 'S'; charCountA[idx]--; } // Leave as 'I' if no available character in A } System.out.println(new String(result)); } }
How This Works With the Examples
Let's take Example 1 to verify:
- Input A:
CLOUD, B:CUPID - Initial countA: C=1, L=1, O=1, U=1, D=1
- After P pass: positions 0 (C=C) and 4 (D=D) are marked P. countA becomes C=0, L=1, O=1, U=1, D=0
- After S pass: position 1 (B's U) has countA[U] = 1, so mark as S. countA[U] becomes 0. Positions 2 (P) and 3 (I) have no matches in A, so stay I.
- Final output:
PSIIPwhich matches the example.
This approach efficiently handles all edge cases, including repeated characters in A and B, while adhering strictly to the priority rules.
内容的提问来源于stack exchange,提问作者Pete Alexandrovich

