您的位置:首页 > 其它

codevs 3301 Square words 题解报告

2016-11-08 11:11 357 查看


噫。

题目描述 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。

啊啊啊啊啊啊啊啊啊啊啊啊啊啊

题目水 数据水 N三方 可以过 啊哈哈



枚举中点,对两侧求LCS;

取最大,

嗯。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cstdlib>
#include<string>
#include<bitset>
#include<iomanip>
#include<deque>
#define INF 1000000000
#define fi first
#define se second
#define N 100005
#define P 1000000007
#define debug(x) cerr<<#x<<"="<<x<<endl
#define MP(x,y) make_pair(x,y)
using namespace std;
int n,m,ans=2147483647;
char s[510];
int f[501][501];
inline int get_num()
{
int num = 0;
char c;
bool flag = false;
while ((c = getchar()) == ' ' || c == '\n' || c == '\r');
if (c == '-') flag = true;
else num = c - '0';
while (isdigit(c = getchar()))
num = num * 10 + c - '0';
return (flag ? -1 : 1) * num;
}
int lcs(int x)
{
memset(f,0,sizeof(f));
for(int i=1;i<x;i++)
{
for(int j=1;j<=n-x+1;j++)
{
if(s[i]==s[j+x-1])
{
f[i][j]=f[i-1][j-1]+1;
}else
{
f[i][j]=max(f[i-1][j],f[i][j-1]);
}
}
}
return f[x-1][n-x+1];
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>s[i];
}
for(int i=2;i<=n;i++)
{
ans=min(ans,n-lcs(i)*2);
}
cout<<ans;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  square noip codevs LCS dp