import java.util.*; public class Main { public...
創建於:2025年7月14日
使用 O4 Mini High 回答 Chat01
創建於:2025年7月14日
使用 O4 Mini High 回答 Chat01
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
int n = s.length();
List<Integer> a = new ArrayList<>();
char p = s.charAt(0);
int l = 1;
for (int i = 1; i < n; i++) {
char c = s.charAt(i);
if (c == p) {
l++;
} else {
a.add(l);
p = c;
l = 1;
}
}
a.add(l);
int flips=0;
for(int i=0;i<n;i+=2){
if(s.charAt(i)!=s.charAt(i+1)) flips++;
}
System.out.println(a+" "+flips);
}
}
i want to reduce the size of a
so basically we are given a binary string s and we have to minimize number of groups(contiguos segment of same element)
ex:00110011 here if we group them the counts will be 2,2,2,2 so 4 groups also the count of each group should be even after minimizing the groups
To minimize the number of even‐sized runs (and secondarily the number of bit‐flips you need), you can treat your string as a sequence of pairs and do a tiny DP over those pairs. Here’s the idea:
the number of flips to turn both bits of pair into .
3. Let
be the best (min segments, then min flips) you can do on the first pairs if pair is set to bit .
4. Recurrence for :
textif b == b': // continuing the same run seg = dpSeg[j-1][b'] flip = dpFlip[j-1][b'] + cost[j][b] else: // starting a new run seg = dpSeg[j-1][b'] + 1 flip = dpFlip[j-1][b'] + cost[j][b]
Take the pair that’s lexicographically smallest over b′∈{0,1}.
5. Initialize at with one segment:
, .
6. At the end pick the better of vs.\ , then backtrack to recover where the segment‐breaks were. Each segment you get spans a certain number of pairs; you multiply by 2 to get your even run‐lengths.
javaimport java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.next(); int n = s.length(); if (n % 2 != 0) { System.out.println("String length must be even"); return; } int m = n / 2; // 1) precompute flip‐cost for each pair j into bit 0 or 1 int[][] cost = new int[m][2]; for (int j = 0; j < m; j++) { char c1 = s.charAt(2*j), c2 = s.charAt(2*j+1); cost[j][0] = (c1!='0' ? 1:0) + (c2!='0' ? 1:0); cost[j][1] = (c1!='1' ? 1:0) + (c2!='1' ? 1:0); } // 2) dpSeg[j][b] = min # of segments up to pair j ending in bit b // dpFlip[j][b] = min flips (tie‐broken after dpSeg) for that choice int[][] dpSeg = new int[m][2]; int[][] dpFlip = new int[m][2]; // to backtrack: what bit we came from int[][] parent = new int[m][2]; // initialize j=0 for (int b = 0; b < 2; b++) { dpSeg[0][b] = 1; dpFlip[0][b] = cost[0][b]; parent[0][b] = -1; } // fill DP for (int j = 1; j < m; j++) { for (int b = 0; b < 2; b++) { int bestSeg = Integer.MAX_VALUE; int bestFlip = Integer.MAX_VALUE; int bestPb = -1; // try previous bit pb for (int pb = 0; pb < 2; pb++) { int seg = dpSeg[j-1][pb] + (b==pb ? 0 : 1); int flip = dpFlip[j-1][pb] + cost[j][b]; // lex compare (seg,flip) if (seg < bestSeg || (seg==bestSeg && flip < bestFlip)) { bestSeg = seg; bestFlip = flip; bestPb = pb; } } dpSeg[j][b] = bestSeg; dpFlip[j][b] = bestFlip; parent[j][b] = bestPb; } } // pick the better ending bit int endBit = (dpSeg[m-1][0] < dpSeg[m-1][1] || (dpSeg[m-1][0]==dpSeg[m-1][1] && dpFlip[m-1][0] <= dpFlip[m-1][1])) ? 0 : 1; // backtrack to find segment boundaries List<Integer> segmentsInPairs = new ArrayList<>(); for (int j = m-1, b = endBit; j >= 0; j--) { int pb = parent[j][b]; // if new segment here? if (j == 0 || b != pb) { segmentsInPairs.add(1); } else { segmentsInPairs.set(segmentsInPairs.size()-1, segmentsInPairs.get(segmentsInPairs.size()-1) + 1); } b = pb; } // reverse to get front‐to‐back Collections.reverse(segmentsInPairs); // each segment is that many pairs → run length = pairs*2 List<Integer> a = new ArrayList<>(); for (int pcount : segmentsInPairs) { a.add(pcount * 2); } // output: the run‐length array and the flips used System.out.println(a + " flips=" + dpFlip[m-1][endBit]); } }
How it works
This runs in time and space and gives you the smallest possible number of even‐length groups.