Reverse Polish Notation
来源:互联网 发布:子域名的作用 编辑:程序博客网 时间:2024/06/05 11:32
http://www.1point3acres.com/bbs/thread-31595-1-1.html
定义一种叫做“Reverse Polish Notation”的表达式:
3 + (4 * 5)
可以写成
3 4 5 * +
即运算符号写在数字的后面。
现在规定,x代表数字,*代表运算符。给定一个包含有x和*的string,问最少需要多少次操作(操作包括,添加一个字符,删除一个字符,替代一个字符)可以使一个string符合Reverse Polish Notation的表达方式。
Input:
第一行: 数字n,代表有n组数据
第2~n+1行:一个包含有x和*的string
Output:
输出一个数字,代表最少需要多少次操作,string可以符合Reverse Polish Notation的表达方式
Sample Input:
5
x
xx*
xxx**
*xx
xx*xx**
Sample Output:
0
0
0
2
0
应该用动态规划做,但是思路还不是很清晰,有空再研究下
递归:
动态规划递归实现。每个xx*运算产生x,设计数器num,指针从字符串开头开始查找,如果找到第一个*,作如下处理:1、若该*前面有2个(或以上)x,删去这两个x和*,首端增加一个x,递归余下字符串;2、若该*前面只有1个x,num++ 1)对应操作:加一个x或删去*,,递归除去*余下部分; 2)对应操作:替换*为x,递归替换后余下部分;3、若该*前面没有x,num++ 1)对应操作:删去*,递归余下部分; 2)对应操作:替换*为x,递归替换后余下部分;如果没有找到而字符串长度>1,num++,加上一个*,递归余下字符串。
public class RPN{public static void main(String[] args){ String[] testCases = { new String("x"), new String("xx"), new String("***"), new String("xxx"), new String("xxx***"), new String("***xxx"), new String("x*xx*"), new String("*xx*x"), }; for (String testCase : testCases){ //TODO check input legal System.out.println(new RPN().operate(testCase)); }}private int operate(String tc){ if (tc.equals("x")) { return 0; }else if (tc.equals("") || tc.equals("*")){ return 1; } int pos = tc.indexOf('*'); if (pos < 0) { return min (operate(tc.substring(1)), operate(tc.substring(2))) + 1; }else if (pos == 0){ return min(operate(tc.substring(pos + 1)), //delete * operate('x' + tc.substring(pos + 1)) //replace * with x ) + 1; }else if (pos == 1){ return min(operate('x' + tc.substring(pos + 1)), //add x or delete * operate("xx" + tc.substring(pos + 1)) //replace * with x ) + 1; }else{ return operate(tc.substring(0, pos - 1) + tc.substring(pos + 1)); }}private static int min(int ...a){ int m = a[0]; for (int k : a){ if (k < m) { m = k; } } return m;}}
动态规划
import java.util.*;import java.math.*;public class Main { private static void set(int[][] dp, int i, int j, int val) { if (dp[i][j] == -1) { dp[i][j] = val; } else { dp[i][j] = Math.min(dp[i][j], val); } } public static void main(String[] args) throws Exception { Scanner scan = new Scanner(System.in); int taskCount = scan.nextInt(); scan.nextLine(); for (int taskIndex = 0; taskIndex < taskCount; taskIndex++) { char[] arr = scan.nextLine().toCharArray(); if (arr.length == 0) { System.out.println("Input string cannot be empty"); continue; } else if (arr.length == 1) { System.out.println(arr[0] == '*' ? 1 : 0); continue; } int[][] dp = new int[arr.length][arr.length]; for (int i = 0; i < dp.length; i++) { Arrays.fill(dp[i], -1); } if (arr[0] == 'x') { dp[0][0] = 0; } else { dp[0][0] = 1; } for (int i = 1; i < arr.length; i++) { if (arr[i] == 'x') { set(dp, i, 1, dp[i - 1][0]); set(dp, i, 0, dp[i - 1][0] + 1); } else { set(dp, i, 1, dp[i - 1][0] + 1); set(dp, i, 0, dp[i - 1][0] + 1); } for (int j = 1; j < arr.length; j++) { if (dp[i - 1][j] == -1) { continue; } if (arr[i] == 'x') { set(dp, i, j + 1, dp[i - 1][j]); set(dp, i, j, dp[i - 1][j] + 1); set(dp, i, j - 1, dp[i - 1][j] + 1); } else { set(dp, i, j + 1, dp[i - 1][j] + 1); set(dp, i, j, dp[i - 1][j] + 1); set(dp, i, j - 1, dp[i - 1][j]); } } } int result = arr.length; for (int i = 0; i < arr.length; i++) { if (dp[dp.length - 1][i] == -1) { continue; } result = Math.min(result, dp[dp.length - 1][i] + i); } System.out.println(result); } }}
我的代码,参考:http://www.careercup.com/question?id=13216725
#include <iostream>#include <stack>#include <stdio.h>#include <string>using namespace std;void minOp(stack<char> &stk, const char *s, int currNum, int &best) { if (stk.size() == 1 && *s == '\0') { if (currNum < best) best = currNum; return; } if (*s == '\0') { if (stk.size() >= 2) { //rm x; or add * stk.pop(); minOp(stk, s, currNum + 1, best); stk.push('x'); } else {//stk.size() == 0 stk.push('x'); minOp(stk, s, currNum + 1, best); stk.pop(); } } else if (*s == '*'){ if (stk.size() < 2) { //rm * minOp(stk, s + 1, currNum + 1, best); //*->x stk.push('x'); minOp(stk, s + 1, currNum + 1, best); stk.pop(); } else { stk.pop(); minOp(stk, s + 1, currNum, best); stk.push('x'); } } else { if (stk.size() >= 2) { //x->* stk.pop(); minOp(stk, s + 1, currNum + 1, best); stk.push('x'); //add * stk.pop(); minOp(stk, s, currNum + 1, best); stk.push('x'); } //rm x minOp(stk, s + 1, currNum + 1, best); //in stack stk.push('x'); minOp(stk, s + 1, currNum, best); stk.pop(); }}int minRPN (const char *s) { int xNum = 0; int nOfDel = 0;//del * int nOfAdd = 0;//add * int nOfRpl = 0;//*->x | x->* while (*s) { if (*s == 'x') { ++xNum; } else { if (xNum == 0) { if (nOfDel >=2) {//如果直接 del *,则又增加了一个操作 nOfDel -= 2; nOfRpl += 2; ++xNum; } else { ++nOfDel;//还可以将*变为x。但是之后有操作,再将del操作替换为replace操作 } } else if(xNum == 1) { if (nOfDel >0) { --nOfDel; ++nOfRpl; } else { ++nOfDel; } } else { --xNum; } } ++s; } while(xNum > 1) { if (xNum >= 3) { ++nOfRpl; xNum -= 2; } else { ++nOfAdd; --xNum; } } return (nOfAdd + nOfDel + nOfRpl);}int main() { string ss[] = {"*","**","***","****","xx*xx**x","xxxxx","***x*"}; for (int i = 0; i < 7; ++i) { stack<char> stk; int best = INT_MAX; minOp(stk, ss[i].c_str(), 0, best); cout << best <<":"<<minRPN(ss[i].c_str())<<endl; } return 0;}
- Reverse Polish Notation
- calculate reverse polish notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- leetcode_Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- 使用extjs4.0实现动态列及分页
- iOS 开发之 CocoaAsyncSocket 学习
- Android之实现定时开关机
- 理解COM套间(第一部分)
- 亮屏灭屏
- Reverse Polish Notation
- static{}(静态代码块)与{}(非静态代码块)
- 保护眼睛(改变窗口颜色,网页和Pdf背景颜色)
- 字节序 大端 小端
- Java词频统计算法(使用单词树)
- 理解COM套间(第二部分)
- 设计模式学习笔记(一) 简单工厂
- http://www.eoeandroid.com/blog-665190-4989.html
- windows 下怎样给你的extjs 项目生成前端api doc