当前位置:网站首页>[code Capriccio - dynamic planning] t392 Judgement subsequence
[code Capriccio - dynamic planning] t392 Judgement subsequence
2022-06-24 03:38:00 【Not blogging】
T392、 Judging subsequences (6/23)
Given string s and t , Judge s Is it t The subsequence .
A subsequence of the string is the original string delete some ( It can also be done without deleting ) A new string of characters formed without changing the relative positions of the remaining characters .( for example ,"ace" yes "abcde" A subsequence of , and "aec" No ).
Advanced :
If there is a lot of input S, Referred to as S1, S2, … , Sk among k >= 10 Billion , You need to check in turn whether they are T The subsequence . under these circumstances , How would you change the code ?
source : Power button (LeetCode)
link :https://leetcode.cn/problems/is-subsequence
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
This problem is the same as solving the longest subsequence , I submitted once according to the code of the longest subsequence , Results the correct .
Later reference code Capriccio ideas , Found a location difference , Is that when
s.charAt(i) != s.charAt(j)
In this case , We can't solve the problem like the original longest subsequence dp[i][j] = max(dp[i-1][j], dp[i][j-1])
It is dp[i][j] = dp[i][j-1]
Because at this time S The string of [0:i-1] No longer T[0:j-1] in , So this is equivalent to t To delete an element ,t If the current element t[j - 1] Delete , that dp[i][j] And the value of that is see s[i - 1] And t[j - 2] The comparison results of , namely :dp[i][j] = dp[i][j - 1];
dp
public boolean isSubsequence(String s, String t) {
int n = s.length();
int m = t.length();
int[][] dp = new int[n+1][m+1];
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(s.charAt(i-1) == t.charAt(j-1)){
dp[i][j] = dp[i-1][j-1] + 1;
}else{
dp[i][j] = dp[i][j-1];
}
}
}
return dp[n][m] == s.length()? true:false;
}
Double finger needling

public boolean isSubsequence(String s, String t) {
int n = s.length();
int m = t.length();
int i = 0, j =0;
while(i < n && j < m){
if(s.charAt(i) == t.charAt(j)){
i++;
}
j++;
}
return i == n?true:false;
}
边栏推荐
- [new double 11] the latest interpretation of Tencent cloud double 11! Get 11000 yuan voucher now!!
- What protocol does FTP belong to in Fortress machine and how to use FTP in Fortress machine
- Tencent cloud ASR product -php realizes the authentication request of the extremely fast version of recording file identification
- How to handle the uplink and downlink silence of TRTC
- 左滑从小窗到大窗口DispatchFrameLayout
- Interpreting Tencent cloud product experience through user experience elements
- Chapter 5: key led demo case of PS bare metal and FreeRTOS case development
- Troubleshooting and resolution of errors in easycvr calling batch deletion interface
- Chapter 6: UART echo case of PS bare metal and FreeRTOS case development
- 高斯光束及其MATLAB仿真
猜你喜欢

你了解TLS协议吗?

Modstartcms enterprise content site building system (supporting laravel9) v4.2.0

浅谈游戏安全 (一)

在pycharm中pytorch的安装

老弹出explorer.exe遇到问题已停止工作,怎么办?

Modstartcms theme introductory development tutorial

halcon知识:区域(Region)上的轮廓算子(2)

ModStartCMS 主题入门开发教程

On Sunday, I rolled up the uni app "uview excellent UI framework"

元气森林推“有矿”,农夫山泉们跟着“卷”?
随机推荐
How to install CentOS 6.5 PHP extension
How to use elastic scaling in cloud computing? What are the functions?
NLP task summary introduction and understanding
ClickHouse Buffer
Recording a summary of frequently asked questions
What is the difference between elasticity and scalability of cloud computing? What does elastic scaling of cloud computing mean?
RI Geng series: write a simple shell script, but it seems to have technical content
Build a small program + management background in 7 days, and this goose factory HR is blessed!
What protocol does FTP belong to in Fortress machine and how to use FTP in Fortress machine
web渗透测试----5、暴力破解漏洞--(3)FTP密码破解
Tencent cloud CIF engineering effectiveness summit was successfully opened, and coding released a series of new products
Can elastic public IP be bound to a home server? The difference between elastic public IP and fixed IP
Grp: how to add Prometheus monitoring in GRP service?
Independent innovation and localization technology: SMT production line monitoring and management visualization of intelligent manufacturing
Troubleshooting and resolution of errors in easycvr calling batch deletion interface
Supply chain system platform: two management areas
What does cloud computing elasticity mean? What are its functions?
Highlights of future cloud native CIF Forum
Tke accesses the cluster through kubectl in pod
How to choose excellent server hosting or server leasing in Beijing