📄 simpledp.java
字号:
}
String ret = "{";
for (int i = 0; i < sLen; ++i) {
ret += kValue + ", ";
}
ret = ret.substring(0, ret.length() - 2) + "}";
return ret;
}
protected Point getCoordsByStep(int step) {
Point ret = new Point();
// Column
int cols = m_s2_size;
ret.x = step % cols + 2;
ret.y = step / cols + 2;
return ret;
}
protected void stepForward(boolean showSteps) {
switch (m_currentPhase) {
case PHASE_CALC_GRID:
if (m_currentStep >=
(m_dpTable.getHCellsCount() - 2) *
(m_dpTable.getVCellsCount() - 2)) {
// Entering backtrack phase
m_currentPhase = PHASE_BACKTRACK;
m_backtrackLastSel = m_dpTable.getLastCell();
m_dpTable.clearDPHighlights();
m_dpTable.clearAllArrows();
// reset result string
for (int i = 0; i < 3; i++) {
m_resLine[i] = "";
}
setInfoMessage("Backtracking Pointers. Policy used: " +
CellElement.getPolicyName(
m_backtrackingPolicy) + ".");
stepFWDBackTrack(showSteps);
}
else {
setInfoMessage("Calculating DP Table. Step: " +
m_currentStep);
stepFWDCalc(showSteps);
}
break;
case PHASE_BACKTRACK:
stepFWDBackTrack(showSteps);
break;
}
}
protected void stepBackward() {
CellElement currentCell;
switch (m_currentPhase) {
case PHASE_CALC_GRID:
m_currentStep--;
if (m_currentStep <= 0) {
stepZero();
return;
}
Point realD = getCoordsByStep(m_currentStep);
currentCell = m_dpTable.getCell(realD.x, realD.y);
// erasing pointers,
// values, etc
currentCell.clearAll();
m_currentStep--;
stepForward(true);
break;
case PHASE_BACKTRACK:
currentCell = (CellElement) m_backTrackList.getLast();
currentCell.clearColor();
if (m_backTrackList.size() <= 1) {
m_currentStep--;
m_currentPhase = PHASE_CALC_GRID;
m_backTrackList.clear();
m_dpTable.clearInteractiveCells();
stepForward(true);
}
else {
m_backTrackList.removeLast();
currentCell = (CellElement) m_backTrackList.getLast();
currentCell.clearColor();
if (m_backTrackList.size() == 0) {
m_backtrackLastSel = m_dpTable.getLastCell();
}
else {
m_backtrackLastSel = (CellElement) m_backTrackList.getLast();
m_backTrackList.removeLast();
}
// cut result string
boolean toErase = false;
for (int i = 0; i < 3; i++) {
if (m_resLine[i].length() > 1) {
m_resLine[i] = m_resLine[i].substring(2,
m_resLine[i].length());
}
else {
m_resLine[i] = "";
toErase = true;
}
}
if (toErase) {
m_bottomResultArea.setText("");
}
stepFWDBackTrack(true);
}
break;
}
}
protected void stepFWDBackTrack(boolean showSteps) {
// Global alignment: start from D(m,n)
// TODO: Not very elegant. To be changed!
CellElement theOneBefore = null;
if (!m_backTrackList.isEmpty()) {
theOneBefore = (CellElement) m_backTrackList.getLast();
}
if (m_backtrackLastSel == null) {
// Policy for automatic pointer selection!!
m_backTrackList.add(theOneBefore.getPointerWithPolicy(
m_backtrackingPolicy));
}
else {
m_backTrackList.add(m_backtrackLastSel);
}
CellElement currentCell = (CellElement) m_backTrackList.getLast();
if (m_backTrackList.size() > 1) {
setResultString(theOneBefore, currentCell);
}
Point D = new Point(currentCell.getColumn() - 1, currentCell.getRow() - 1);
m_l1Choiche.setBackground(m_mainPane.getBackground());
m_l2Choiche.setBackground(m_mainPane.getBackground());
m_l3Choiche.setBackground(m_mainPane.getBackground());
CellElement leftCell = currentCell.getLeftPointer();
CellElement topCell = currentCell.getTopPointer();
CellElement topLeftCell = currentCell.getDiagPointer();
String DEqual = "D(" + (D.y) + ", " + (D.x) + ") = Select";
String DLeft = "";
String DTop = "";
String DTopLeft = "";
m_dpTable.clearInteractiveCells();
// Init choosen array
LinkedList highlightList = new LinkedList();
if (leftCell == null) {
DLeft = "No Pointer";
}
else {
DLeft = "D(" + (leftCell.getRow() - 1) + ", " +
(leftCell.getColumn() - 1) + ")";
m_dpTable.addInteractiveCell(leftCell);
highlightList.add(leftCell);
}
if (topCell == null) {
DTop = "No Pointer";
}
else {
DTop = "D(" + (topCell.getRow() - 1) + ", " +
(topCell.getColumn() - 1) + ")";
m_dpTable.addInteractiveCell(topCell);
highlightList.add(topCell);
}
if (topLeftCell == null) {
DTopLeft = "No Pointer";
}
else {
DTopLeft = "D(" + (topLeftCell.getRow() - 1) + ", " +
(topLeftCell.getColumn() - 1) + ")";
m_dpTable.addInteractiveCell(topLeftCell);
highlightList.add(topLeftCell);
}
m_lDEqual.setText(DEqual);
m_l1Choiche.setText(DLeft);
m_l2Choiche.setText(DTop);
m_l3Choiche.setText(DTopLeft);
m_dpTable.setTriArrows(currentCell, false);
m_dpTable.setMultipleCellHighlight(highlightList);
currentCell.setColor(Color.green);
if (currentCell.getColumn() == 1 &&
currentCell.getRow() == 1) {
m_btnNext.setEnabled(false);
m_btnEnd.setEnabled(false);
m_dpTable.clearAllArrows();
m_dpTable.clearGridCircle();
}
else {
// TODO: Not elegant. To be changed
m_btnNext.setEnabled(true);
m_btnEnd.setEnabled(true);
}
if (showSteps) {
m_dpTable.paint(m_dpTable.getGraphics());
}
m_backtrackLastSel = null;
}
protected void setResultString(CellElement prevCell,
CellElement selectedBackTrackingCell) {
Point prevPos = new Point(prevCell.getColumn() - 2,
prevCell.getRow() - 2);
/** I.e.
* S1 = CATAGTG
* S2 = GTCAGGT
*
* Possible Aligment:
* --CATAGTG
* || ||
* GTCAG-GT-
*/
switch (prevCell.getPointerPos(selectedBackTrackingCell)) {
// LEFT or TOP alighment are Insertion/Suppression
case CellElement.POINTERPOS_LEFT:
m_resLine[0] = "-" + m_resLine[0];
m_resLine[1] = " " + m_resLine[1];
m_resLine[2] = m_s2.charAt(prevPos.x) + m_resLine[2];
break;
case CellElement.POINTERPOS_TOP:
m_resLine[0] = m_s1.charAt(prevPos.y) + m_resLine[0];
m_resLine[1] = " " + m_resLine[1];
m_resLine[2] = "-" + m_resLine[2];
break;
// DIAG is substitution (if the two chars are different)
// or identity (if the two chars are equal)
case CellElement.POINTERPOS_DIAG:
m_resLine[0] = m_s1.charAt(prevPos.y) + m_resLine[0];
if (m_s1.charAt(prevPos.y) == m_s2.charAt(prevPos.x)) {
m_resLine[1] = "|" + m_resLine[1];
}
else {
m_resLine[1] = " " + m_resLine[1];
}
m_resLine[2] = m_s2.charAt(prevPos.x) + m_resLine[2];
break;
}
m_bottomResultArea.setText(m_resLine[0] + "\n" +
m_resLine[1] + "\n" +
m_resLine[2] + "\n");
}
public void onInteractPress(CellElement cellPressed) {
// do the interact things
m_backtrackLastSel = cellPressed;
stepFWDBackTrack(true);
}
protected void stepFWDCalc(boolean showSteps) {
if (m_currentStep == 0) {
m_btnSetOne.setEnabled(false);
m_btnSetTwo.setEnabled(false);
m_btnSetGapOne.setEnabled(false);
m_btnSetGapTwo.setEnabled(false);
m_btnPrev.setEnabled(true);
m_btnBeginning.setEnabled(true);
}
Point realD = getCoordsByStep(m_currentStep);
Point D = new Point(realD.x - 1, realD.y - 1);
int p = 1;
m_l1Choiche.setBackground(m_mainPane.getBackground());
m_l2Choiche.setBackground(m_mainPane.getBackground());
m_l3Choiche.setBackground(m_mainPane.getBackground());
CellElement leftCell = m_dpTable.getCell(realD.x - 1, realD.y);
CellElement topCell = m_dpTable.getCell(realD.x, realD.y - 1);
CellElement topLeftCell = m_dpTable.getCell(realD.x - 1, realD.y - 1);
CellElement currentCell = m_dpTable.getCell(realD.x, realD.y);
if (m_s2.charAt(realD.x - 2) == m_s1.charAt(realD.y - 2)) {
p = 0;
}
if (showSteps) {
String DEqual = "D(" + (D.y) + ", " + (D.x) + ") = Min";
String DLeft = "D(" + (D.y - 1) + ", " + (D.x) + ") + 1 = " +
leftCell.getVal() + " + 1 = " + (leftCell.getIntVal() + 1);
String DTop = "D(" + (D.y) + ", " + (D.x - 1) + ") + 1 = " +
topCell.getVal() + " + 1 = " + (topCell.getIntVal() + 1);
String DTopLeft = "D(" + (D.y - 1) + ", " + (D.x - 1) + ") + " + p +
" = " +
topLeftCell.getVal() + " + " + p + " = " +
(topLeftCell.getIntVal() + p);
m_lDEqual.setText(DEqual);
m_l1Choiche.setText(DLeft);
m_l2Choiche.setText(DTop);
m_l3Choiche.setText(DTopLeft);
}
int fromLeftVal = leftCell.getIntVal() + 1;
int fromTopVal = topCell.getIntVal() + 1;
int fromTopLeftVal = topLeftCell.getIntVal() + p;
int min = Math.min(fromLeftVal, Math.min(fromTopVal, fromTopLeftVal));
// Init choosen array
LinkedList highlightList = new LinkedList();
if (fromLeftVal == min) {
m_l1Choiche.setBackground(Color.yellow);
currentCell.addLeftPointer(leftCell);
highlightList.add(leftCell);
}
if (fromTopVal == min) {
m_l2Choiche.setBackground(Color.yellow);
currentCell.addTopPointer(topCell);
highlightList.add(topCell);
}
if (fromTopLeftVal == min) {
m_l3Choiche.setBackground(Color.yellow);
currentCell.addDiagPointer(topLeftCell);
highlightList.add(topLeftCell);
}
currentCell.setIntVal(min);
if (showSteps) {
if (p == 0) {
// the two characters are equal: RED
m_dpTable.setSideHighlight(currentCell, Color.red);
}
else {
// the two characters are not equal:
m_dpTable.setSideHighlight(currentCell, new Color(0, 255, 255));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -