当前位置:网站首页>[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
边栏推荐
- 【科普】热设计基础知识:5G光器件之散热分析
- 假设检验——《概率论与数理统计》第八章学习笔记
- Power management bus (pmbus)
- How to remove installed elpa package
- Hexadecimal to octal
- Cookie learning diary 1
- Neural networks and deep learning Chapter 6: Circular neural networks reading questions
- PHP读取ini文件并修改内容写入
- About the prompt loading after appscan is opened: guilogic, it keeps loading and gets stuck. My personal solution. (it may be the first solution available in the whole network at present)
- Live broadcast preview | container service ack elasticity prediction best practice
猜你喜欢
Function (basic: parameter, return value)
Threejs Internet of things, 3D visualization of farms (I)
Interview related high-frequency algorithm test site 3
【FineBI】使用FineBI制作自定义地图过程
MacBook installation postgresql+postgis
托管式服务网络:云原生时代的应用体系架构进化
MacBook安装postgreSQL+postgis
10 programming habits that web developers should develop
【科普】热设计基础知识:5G光器件之散热分析
Decryption function calculates "task state and lifecycle management" of asynchronous task capability
随机推荐
[phantom engine UE] realize the animation production of mapping tripod deployment
Network layer - forwarding (IP, ARP, DCHP, ICMP, network layer addressing, network address translation)
Learning notes 8
机器学习 --- 决策树
3 minutes learn to create Google account and email detailed tutorial!
windows下Redis-cluster集群搭建
The remainder operation is a hash function
This is an age of uncertainty
Introduction to RT thread kernel (5) -- memory management
[popular science] basic knowledge of thermal design: heat dissipation analysis of 5g optical devices
Ffmepg usage guide
首席信息官如何利用业务分析构建业务价值?
程序员应该怎么学数学
Fuel consumption calculator
After the deployment of web resources, the navigator cannot obtain the solution of mediadevices instance (navigator.mediadevices is undefined)
[finebi] the process of making custom maps using finebi
FFmepg使用指南
Scope of package class package
【科普】热设计基础知识:5G光器件之散热分析
取余操作是一个哈希函数