For two integers a and b , we define f(a,b) as t...
생성일: 2025년 6월 17일
답변에 사용된 모델 O4 Mini High by Chat01
생성일: 2025년 6월 17일
답변에 사용된 모델 O4 Mini High by Chat01
For two integers a
and b
, we define f(a,b)
as the number of positions in the decimal representation of the numbers a
and b
where their digits are the same. For example, f(12,21)=0
, f(31,37)=1
, f(19891,18981)=2
, f(54321,24361)=3
.
You are given two integers l
and r
of the same length in decimal representation. Consider all integers l≤x≤r
. Your task is to find the minimum value of f(l,x)+f(x,r)
.
Input
Each test consists of multiple test cases. The first line contains a single integer t
(1≤t≤104
) — the number of test cases. The description of the test cases follows.
Each test case consists of a single line containing two integers l
and r
(1≤l≤r<109
).
It is guaranteed that the numbers l
and r
have the same length in decimal representation and do not have leading zeros.
Output
For each test case, output the minimum value of f(l,x)+f(x,r)
among all integer values l≤x≤r
.
Example
InputCopy
14
1 1
2 3
4 6
15 16
17 19
199 201
899 999
1990 2001
6309 6409
12345 12501
19987 20093
746814 747932
900990999 900991010
999999999 999999999
OutputCopy
2
1
0
3
2
2
1
3
3
4
3
5
12
18
Note
In the first test case, you can choose x=1
. Then f(1,1)+f(1,1)=1+1=2
.
In the second test case, you can choose x=2
. Then f(2,2)+f(2,3)=1+0=1
.
In the third test case, you can choose x=5
. Then f(4,5)+f(5,6)=0+0=0
.
In the fourth test case, you can choose x=15
. Then f(15,15)+f(15,16)=2+1=3
.
In the fifth test case, you can choose x=18
. Then f(17,18)+f(18,19)=1+1=2
.
In the sixth test case, you can choose x=200
. Then f(199,200)+f(200,201)=0+2=2
.
In the seventh test case, you can choose x=900
. Then f(899,900)+f(900,999)=0+1=1
.
In the eighth test case, you can choose x=1992
. Then f(1990,1992)+f(1992,2001)=3+0=3
.
code in java
javaimport java.io.*; import java.util.*; public class Main { static String L, R; static int N; // memo[pos][tightLow?1:0][tightHigh?1:0] static int[][][] memo; static int[] lDigits, rDigits; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int t = Integer.parseInt(br.readLine().trim()); while (t-- > 0) { StringTokenizer st = new StringTokenizer(br.readLine()); L = st.nextToken(); R = st.nextToken(); N = L.length(); lDigits = new int[N]; rDigits = new int[N]; for (int i = 0; i < N; i++) { lDigits[i] = L.charAt(i) - '0'; rDigits[i] = R.charAt(i) - '0'; } memo = new int[N][2][2]; for (int i = 0; i < N; i++) { for (int a = 0; a < 2; a++) { for (int b = 0; b < 2; b++) { memo[i][a][b] = -1; } } } int answer = dp(0, 1, 1); out.println(answer); } out.flush(); } // pos: current digit position [0..N) // tightLow: if prefix so far equals L's prefix (1=true, 0=false) // tightHigh: if prefix so far equals R's prefix (1=true, 0=false) static int dp(int pos, int tightLow, int tightHigh) { if (pos == N) { return 0; } if (memo[pos][tightLow][tightHigh] != -1) { return memo[pos][tightLow][tightHigh]; } int res = Integer.MAX_VALUE; int low = tightLow == 1 ? lDigits[pos] : 0; int high = tightHigh == 1 ? rDigits[pos] : 9; for (int d = low; d <= high; d++) { int nextTightLow = (tightLow == 1 && d == lDigits[pos]) ? 1 : 0; int nextTightHigh = (tightHigh == 1 && d == rDigits[pos]) ? 1 : 0; int cost = 0; if (d == lDigits[pos]) cost++; if (d == rDigits[pos]) cost++; int sub = dp(pos + 1, nextTightLow, nextTightHigh); res = Math.min(res, cost + sub); } memo[pos][tightLow][tightHigh] = res; return res; } }
Explanation of the approach:
dp(pos, tightLow, tightHigh)
be the minimum total cost from digit position pos
to the end, given whether we’re still “tight” to the lower bound (tightLow
) and to the upper bound (tightHigh
).This solves for the minimum value of over all .