3301 Square words
2024-08-27 12:38:00
题目描述 Description
定义square words为:
1.长度为偶数。
2.前一半等于后一半。
比如abcabc和aaaa都是square words,但是abcabcab和aaaaa都不是。
现在有一个长度为n的字符串,求至少要删掉多少个字符,使得剩下的字符串是square words。
输入描述 Input Description
第一行包含一个正整数n。
第二行一个长度为n的字符串,仅包含小写字母
输出描述 Output Description
仅包含一个整数,表示最少需要删掉的字符数
样例输入 Sample Input
11
abaccdaabcd
样例输出 Sample Output
3
数据范围及提示 Data Size & Hint
【样例说明】
abaccdaabcd
【数据规模】
对于40%的数据,n ≤ 20;
对于100%的数据,n ≤ 500。
分类标签 Tags 点此展开
题解:
O(n^3)的最长公共子序列,居然过了。
AC代码:
#include<cstdio>
#include<string>
#include<iostream>
using namespace std;
const int N=1e3+;
int n,f[N][N];
string s;
int ans=0x7fffffff;
inline int LCS(string a,string b){
int l1=a.length(),l2=b.length();
for(int i=;i<l1;i++){
for(int j=;j<l2;j++){
if(!i||!j) f[i][j]=a[i]==b[j];
else{
if(a[i]==b[j]) f[i][j]=f[i-][j-]+;
else f[i][j]=max(f[i-][j],f[i][j-]);
}
}
}
return f[l1-][l2-];
}
int main(){
cin>>n>>s;
for(int i=;i<n-;i++){
string a,b;
for(int j=;j<=i;j++) a+=s[j];
for(int j=i+;j<n;j++) b+=s[j];
int anc=LCS(a,b);
ans=min(ans,n-*anc);
}
printf("%d\n",ans);
return ;
}
最新文章
- 了解一下JavaScript的未来——ECMAScript5
- tar
- XML 增删改查
- Linux(Centos、Debian)之安装Java JDK及注意事项(转)
- Android调整TimePicker和DatePicker大小
- C#中int32 的有效值范围
- OGC 的WCS WFS 及WMS 服务
- LNMP环境的安装
- Maven生成可以直接运行的jar包的多种方式
- Linux基础命令归纳大全
- ArrayBlockingQueue简介
- 深入分析Parquet列式存储格式【转】
- CS229 - MachineLearning - 12 强化学习笔记
- mysql慢查询日志功能的使用
- UITableView:改变 TableHeaderView 的高度
- c++ 11和java 8都支持lambda表达式
- js url转码
- 23. Man and His Natural Habitat 人类及其自然栖息地
- HTTP请求中的Keep-Alive模式详解
- MSSQL中通过关键字查找所有存储过程
热门文章
- [Windows Server 2012] 更改服务器密码
- apache自带的ab压力测试工具
- HDU_1180_诡异的楼梯_BFS
- Extensions can add new functionality to a type, but they cannot override existing functionality.
- First Project -用函数写的ATM+购物商城程序
- CAD控件:梦想CAD控件功能更新 清除图上的所有高亮实体
- Compute和Linq的Field使用
- iOS APP EuclidStudy Service Support
- react 导航切换
- 洛谷——P1349 广义斐波那契数列(矩阵加速)