关于字符串的简单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.从流中读取数据者 ...
- ECMAScript 6 入门之变量的解构赋值
1.数组 1. var a=1,b=2,c=3; console.log("a:",a) console.log("b:",b) console.log(&qu ...
- 【Unity】Protobuf的使用与常见问题
Protobuf的使用流程 protobuf参考教程:https://www.jianshu.com/p/b135676dbe8d 手写.proto文件后,用CMD命令行运行protoc.exe编译器 ...
- unable to locate package gparted
在unbuntu安装gparted的时候出现这个错误提示,意思为:找不到这个安装包 可能的原因: 1.当前系统更新包没有更新,执行命令:sudo apt-get update 2.命令错误,重新检查需 ...
- Android WiFi 获取国家码
记录一下Android获取国家码的方式 Wifi 国家码获取途径 1.DefaultCountryTablefield in WCNSS_qcom_wlan_nv.bin-read during dr ...
- MAC下Myeclipse SVN插件安装
1.下载SVN插件包:http://download.csdn.net/detail/frankyanchen/4512899 2.在myeclipse文件夹下创建一个文件夹为svntool并复制下载 ...
- Writing DynamicTableEntity to Azure Storage Table
There are ample of samples available to show how to insert an object/entity to Azure Storage Table. ...
- [转]如何通过 App Store 审核(iOS 开发者经验分享)
CocoaChina交流社区曾经发起一个主题讨论,关于iOS开发者遇到审核失败的原因及解决办法的,有价值的回复内容如下: wubo9935 App中设计的图标与Apple原生图标类似,Apple原生图 ...
- traff.sh
#!/bin/bash #网络监控 while [ "1" ] do eth=$1 RXpre=$(cat /proc/net/dev | grep $eth | tr : &qu ...
- 十二、K3 WISE 开发插件《工业单据老单与自己添加的窗体 - 互相传值传参》
===================================== 目录: 1.演示效果--[销售订单]传值给[自定义窗体] 2.演示效果--[自定义窗体]传值给[销售订单] 3.附源码 4. ...