string 操作

Posted on   jacyoier  阅读(7)  评论(0编辑  收藏  举报
复制代码
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4     ios::sync_with_stdio(false);
 5     string s1="what about to ask ",s2="Mike telephone number";
 6     //1
 7     s1.replace(11,16,"asking");
 8     cout<<s1<<endl;
 9     
10     //2
11     s2=s2.insert(5,"for ");
12     cout<<s2<<endl;
13     
14     //3
15     string s=s1+s2;
16     cout<<s<<endl;
17     
18     //4
19     if(s2.find("Mike")!=s2.npos){
20         cout<<true<<endl;
21     }
22     else{
23         cout<<false<<endl;
24     }
25     
26     if(s.find("Mike")!=s.npos){
27         cout<<true<<endl;
28     }
29     else{
30         cout<<false<<endl;
31     }
32     
33     //5
34     if(s2.find("z")!=s2.npos){
35         cout<<true<<endl;
36     }
37     else{
38         cout<<false<<endl;
39     }
40     
41     return 0;
42 }
复制代码

 

 

最长公共子序列:

复制代码
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N=1010;
 4 string s1,s2;
 5 int len1,len2,f[N][N];
 6 int main(){
 7     ios::sync_with_stdio(false);
 8     cin>>s1>>s2;
 9     len1=s1.length();
10     len2=s2.length();
11     for(int i=1;i<=len1;i++){
12         for(int j=1;j<=len2;j++){
13             if(s1[i-1]==s2[j-1]) f[i][j]=f[i-1][j-1]+1;
14             else f[i][j]=max(f[i-1][j],f[i][j-1]);
15         }
16     }
17     cout<<f[len1][len2];
18     return 0;
19 }
复制代码

 

最长上升子序列:

复制代码
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4     ios::sync_with_stdio(false);
 5     int a[10005];
 6     int dp[10005];
 7     int n=1,lt,rt,mid;
 8     int max;
 9     while(cin>>a[n]) n++;
10     max=1;
11     dp[max]=a[1];
12     for(int i=2;i<n;i++){
13         if(a[i]>dp[max]){
14             max++;
15             dp[max]=a[i];
16         }else{
17             lt=1;
18             rt=max;
19             while(lt<rt){
20                 mid=(lt+rt)/2;
21                 if(a[i]>dp[mid]){
22                     lt=mid+1;
23                 } else{
24                     rt=mid;
25                 }
26             }
27             dp[lt]=a[i];
28         }
29     }
30     for(int i=1;dp[i]!=0;i++){
31         cout<<dp[i]<<" ";
32     }
33     return 0;
34 }
复制代码

 

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)

随笔 - 15, 文章 - 2, 评论 - 0, 阅读 - 983

Copyright © 2025 jacyoier
Powered by .NET 9.0 on Kubernetes

点击右上角即可分享
微信分享提示