关于字符串的简单dp

看这道题题目叫做魔族密码多新奇的名字点开是道字符串的dp,思考然后想出lis其实但字符串之间的比对只有循环然后其实循环爆不了,太懒点开了题解发现有人使用stl——cstring的函数了方便多了,借鉴一下好啦。
函数:strncmp这个是cstring里对比两个字符串的东西从第一位开始对照相同则返回0,不同返回差值。
用法:strncmp(第一个字符串,第二个字符串,要比对的长度);
然后还有返回字符串长度的东西strlen(某个字符串的长度);这样加上lis就很好过啦。
然后scanf输入字符的时候,%s是输入字符串后面不加&,而%c输入单个字符后面加&。代码中将char类型直接当做字符串使用了,蒟蒻没见过的新操作诶,直接把char当做字符串。还有别忘了加string。字符串的题也得多做,要不容易遗忘一些简单的操作。
#include<iostream>
#include<cstdio>
#include<map>
#include<vector>
#include<iomanip>
#include<cmath>
#include<ctime>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
const int maxn=;
int n,ans=;
char s[maxn][];
int f[maxn];
int main()
{
//freopen("1.in","r",stdin);
n=read();
for(int i=;i<=n;i++)scanf("%s",s[i]);
for(int i=;i<=n;i++)f[i]=;
for(int i=;i<=n;i++)
{
for(int j=;j<i;j++)
{
if(strncmp(s[i],s[j],strlen(s[j]))==)
f[i]=max(f[i],f[j]+);
}
}
for(int i=;i<=n;i++)
ans=max(ans,f[i]);
printf("%d\n",ans);
return ;
}
题交后wa了3个点发现自己赋初值赋的不对这个一定要注意每个f[i]都应该是1;失误。
不加函数的也就多了一个for循环而已。自己太懒啦。。。
#include<iostream>
#include<cstdio>
#include<map>
#include<vector>
#include<iomanip>
#include<cmath>
#include<ctime>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
const int maxn=;
int n,ans=;
char s[maxn][];
int f[maxn];
int main()
{
//freopen("1.in","r",stdin);
n=read();
for(int i=;i<=n;i++)scanf("%s",s[i]);
for(int i=;i<=n;i++)f[i]=;
for(int i=;i<=n;i++)
{
for(int j=;j<i;j++)
{
int flag=;
for(int k=;k<strlen(s[j]);k++)
{
if(s[j][k]!=s[i][k])
flag=;
}
if(flag==)f[i]=max(f[i],f[j]+);
}
}
for(int i=;i<=n;i++)
ans=max(ans,f[i]);
printf("%d\n",ans);
return ;
}
谁的青春不曾有过潦草迷茫?
关于字符串的简单dp的更多相关文章
- 字符串距离 简单DP
字符串距离 时间限制: 1 Sec 内存限制: 128 MB 题目描述 设有字符串 X,我们称在 X 的头尾及中间插入任意多个空格后构成的新字符串为 X 的扩展串,如字符串 X 为"abc ...
- UVA - 11584 划分字符串的回文串子串; 简单dp
/** 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34398 UVA - 11584 划分字符串的回文串子串: 简单 ...
- 简单 dp
1.摆花问题 题目描述小明的花店新开张,为了吸引顾客,他想在花店的门口摆上一排花,共m盆.通过调查顾客的喜好,小明列出了顾客最喜欢的n种花,从1到n标号.为了在门口展出更多种花,规定第i种花不能超过a ...
- HDOJ 1501 Zipper 【简单DP】
HDOJ 1501 Zipper [简单DP] Problem Description Given three strings, you are to determine whether the th ...
- 简单dp总结
### 简单dp总结 本文是阅读<挑战程序设计第二版>其中关于dp章节所作总结.将简要描述dp的部分知识. 一.dp是什么? dp在计算机专业学科中全称是动态规划(dynamic prog ...
- Codeforces Round #394 (Div. 2) C. Dasha and Password(简单DP)
C. Dasha and Password time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- hdu1501 Zipper[简单DP]
目录 题目地址 题干 代码和解释 参考 题目地址 hdu1501 题干 代码和解释 最优子结构分析:设这三个字符串分别为a.b.c,如果a.b可以组成c,那么c的最后一个字母必定来自a或者b的最后一个 ...
- 4.15 每周作业 —— 简单DP
免费馅饼 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submissi ...
- HDU 1087 简单dp,求递增子序列使和最大
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
随机推荐
- epoll的由来
reference https://www.zhihu.com/question/20122137 感谢 @静海听风 @蓝形参 数据流有两个重要的参与者: 1.往流中写入数据者 2.从流中读取数据者 ...
- 推荐几个Windows工具软件: HDDB - Everything的替代品
主页: http://hddb.xp-zed.com/index.html 简单地说,HDDB 是NTFS磁盘的文件搜索工具,它最开始是模仿 Everything 的,但解决了 Everything ...
- 业界常用的和不常用cad快捷键
AutoCAD 是目前世界各国工程设计人员的首选设计软件,简便易学.精确无误是AutoCAD成功的两个重要原因.AutoCAD提供的命令有很多,绘图时最常用的命令只有其中的百分之二十. 在CAD软件操 ...
- npm国内镜像
国内使用默认的源安装较慢,镜像使用方法(三种办法任意一种都能解决问题,建议使用第三种,将配置写死,下次用的时候配置还在): 1.通过config命令 npm config set registry h ...
- redis Sentinel部署
sentinel 系统用于管理多个 Redis 服务器(instance) 执行以下三个任务: 监控(Monitoring): Sentinel 会不断地检查你的主服务器和从服务器是否运作正常. 提醒 ...
- java二叉树字典查询(qq 928900200)
This assignment will help you practice and understand better the Binary Tree and Binary Search Tree ...
- 在windows下执行./configure,make,makeinstall源码安装程序spice-gtk
使用MSYS软件,在我的上一篇博客中有软件下载地址.本文使用MSYS进行源码编译spice-gtk-0.33. 首先打开MSYS软件,进入你源码所在目录,例如:cd /c/Users/Admi... ...
- Cocos2d-x 3.0 纹理
1.纹理控制. Sprite *pSprite = Sprite::create("background.png"); TexParams params = {GL_NEAREST ...
- Java如何创建多线程服务器?
在Java编程中,如何创建多线程服务器? 以下示例演示如何使用ServerSocket类的MultiThreadServer(socketname)方法和Socket类的ssock.accept()方 ...
- 一个手写的 http client
public class HTTPClient { public static final String GET = "GET"; public static final Stri ...