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 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)