当前位置:网站首页>[crampon programming] lintcode decoding Encyclopedia - 1100 strange printer
[crampon programming] lintcode decoding Encyclopedia - 1100 strange printer
2022-07-05 04:31:00 【BZIClaw】
【 Crampon programming 】LintCode Decoding Encyclopedia —— 1100 Strange printer
author :BZIClaw
1100 Strange printer
Python:
class Solution:
""" @param s: @return: the minimum number of turns the printer needed in order to print it """
def strangePrinter(self, s):
# write your code here
dp = [[0 for i in range(0,102)] for i in range(0,102)]
n = len(s)
if n == 0:
return 0
for i in range(0,n):
dp[i][i] = 1
for leng in range(1,n):
for j in range(0,n-leng):
dp[j][j+leng] = leng + 1
for k in range(j+1,j+leng+1):
temp = dp[j][k-1] + dp[k][j+leng]
if s[k-1] == s[j+leng]:
temp -= 1
dp[j][j+leng] = min(dp[j][j+leng],temp)
return dp[0][n-1]
Java:
public class Solution {
/** * @param s: * @return: the minimum number of turns the printer needed in order to print it */
public int strangePrinter(String s) {
// write your code here
int n = s.length();
int[][] f = new int[n][n];
for (int i = n - 1; i >= 0; i--) {
f[i][i] = 1;
for (int j = i + 1; j < n; j++) {
if (s.charAt(i) == s.charAt(j)) {
f[i][j] = f[i][j - 1];
} else {
int minn = Integer.MAX_VALUE;
for (int k = i; k < j; k++) {
minn = Math.min(minn, f[i][k] + f[k + 1][j]);
}
f[i][j] = minn;
}
}
}
return f[0][n - 1];
}
}
C++
class Solution {
public:
/** * @param s: * @return: the minimum number of turns the printer needed in order to print it */
// Interval class DP
//DFS
vector<vector<int>> dp;
int strangePrinter(string &s) {
// write your code here
dp = vector<vector<int>>(s.length(), vector<int>(s.length(), -1));
return strangePrinter(s, 0, s.length() - 1);
}
int strangePrinter(string &s, int l, int r){
if(l > r)
return 0;
if(dp[l][r] != -1)
return dp[l][r];
// Default behavior, print s[i] to s[j - 1] and print s[j]
int ret = strangePrinter(s, l, r - 1) + 1;
for(int i = l; i < r; i++){
if(s[i] == s[r])
ret = min(ret, strangePrinter(s, l, i) + strangePrinter(s, i + 1, r - 1));
}
return dp[l][r] = ret;
}
};
JavaScript:
export class Solution {
/** * strangePrinter * * @param s: * @return: the minimum number of turns the printer needed in order to print it */
strangePrinter(s) {
if ( !s ) {
return 0; }
let n = s.length;
let f = [];
for (let i = 0; i < n; i++) {
f[i] = [];
f[i][i] = 1;
}
for (let l = 2; l <= n; l++ ) {
for (let i = 0; i + l <= n; i++) {
let j = i + l - 1;
f[i][j] = 1 + f[i+1][j];
for (let k = i + 1; k < j; k++) {
if (s[i] == s[k]) {
f[i][j] = Math.min(f[i][j], f[i+1][k] + f[k+1][j]);
}
}
if (s[i] == s[j]) {
f[i][j] = Math.min(f[i][j], f[i+1][j]);
}
}
}
return f[0][n-1];
}
}
Go:None
Pay attention to me , Learn more about programming
边栏推荐
- Practice | mobile end practice
- Fuel consumption calculator
- 可观测|时序数据降采样在Prometheus实践复盘
- Neural network and deep learning Chapter 1: introduction reading questions
- 包 类 包的作用域
- windows下Redis-cluster集群搭建
- Decimal to hexadecimal
- What are the building energy-saving software
- 【虚幻引擎UE】实现测绘三脚架展开动画制作
- Mixed compilation of C and CC
猜你喜欢
Live broadcast preview | container service ack elasticity prediction best practice
Seven join join queries of MySQL
TPG x AIDU|AI领军人才招募计划进行中!
[finebi] the process of making custom maps using finebi
Mxnet imports various libcudarts * so、 libcuda*. So not found
如何优雅的获取每个分组的前几条数据
【FineBI】使用FineBI制作自定义地图过程
可观测|时序数据降采样在Prometheus实践复盘
Hypothesis testing -- learning notes of Chapter 8 of probability theory and mathematical statistics
【虚幻引擎UE】实现测绘三脚架展开动画制作
随机推荐
Neural networks and deep learning Chapter 4: feedforward neural networks reading questions
Mxnet imports various libcudarts * so、 libcuda*. So not found
Hexadecimal to decimal
【虚幻引擎UE】实现背景模糊下近景旋转操作物体的方法及踩坑记录
Stage experience
This is an age of uncertainty
Threejs Internet of things, 3D visualization of farms (I)
官宣!第三届云原生编程挑战赛正式启动!
A application wakes up B should be a fast method
概率论与数理统计考试重点复习路线
Decimal to hexadecimal
如何优雅的获取每个分组的前几条数据
PR video clip (project packaging)
【科普】热设计基础知识:5G光器件之散热分析
File upload bypass summary (upload labs 21 customs clearance tutorial attached)
3 minutes learn to create Google account and email detailed tutorial!
Rome chain analysis
Here comes the Lantern Festival red envelope!
Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery
【虚幻引擎UE】实现测绘三脚架展开动画制作