3301 Square words

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 钻石 Diamond
 查看运行结果
 
 
题目描述 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 ;
}

最新文章

  1. 了解一下JavaScript的未来——ECMAScript5
  2. tar
  3. XML 增删改查
  4. Linux(Centos、Debian)之安装Java JDK及注意事项(转)
  5. Android调整TimePicker和DatePicker大小
  6. C#中int32 的有效值范围
  7. OGC 的WCS WFS 及WMS 服务
  8. LNMP环境的安装
  9. Maven生成可以直接运行的jar包的多种方式
  10. Linux基础命令归纳大全
  11. ArrayBlockingQueue简介
  12. 深入分析Parquet列式存储格式【转】
  13. CS229 - MachineLearning - 12 强化学习笔记
  14. mysql慢查询日志功能的使用
  15. UITableView:改变 TableHeaderView 的高度
  16. c++ 11和java 8都支持lambda表达式
  17. js url转码
  18. 23. Man and His Natural Habitat 人类及其自然栖息地
  19. HTTP请求中的Keep-Alive模式详解
  20. MSSQL中通过关键字查找所有存储过程

热门文章

  1. [Windows Server 2012] 更改服务器密码
  2. apache自带的ab压力测试工具
  3. HDU_1180_诡异的楼梯_BFS
  4. Extensions can add new functionality to a type, but they cannot override existing functionality.
  5. First Project -用函数写的ATM+购物商城程序
  6. CAD控件:梦想CAD控件功能更新 清除图上的所有高亮实体
  7. Compute和Linq的Field使用
  8. iOS APP EuclidStudy Service Support
  9. react 导航切换
  10. 洛谷——P1349 广义斐波那契数列(矩阵加速)