ural1238. Folding(记忆化)
这算模拟加记忆化吗 找bug找了2个多小时。。记忆化部分好想 就是字符串处理部分挫了 一个个复制模拟 各种修改查找
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
using namespace std;
#define INF 0xfffffff
int dp[][],o[][];
char s[][][],ss[];
int len(int x)
{
int q=;
while(x)
{
q++;x/=;
}
return q;
}
int dfs(int x,int y)
{
if(dp[x][y]!=INF) return dp[x][y];
if(x==y)
{
s[x][y][] = ss[x];
s[x][y][] = '\0';
o[x][y] = ;
return dp[x][y] = ;
}
int i,j,flag,k;
for(i = x ; i < y ; i++)
{
dp[x][i] = dfs(x,i);
dp[i+][y] = dfs(i+,y);
flag = ;
k = INF;
if(strcmp(s[x][i],s[i+][y])==)
{
int t = ;
if(o[x][i]+o[i+][y]>)
t+=;
t+=len(o[x][i]+o[i+][y]);
k = strlen(s[x][i])+t;flag = ;
}
if(dp[x][i]+dp[i+][y]<k)
{
k = dp[x][i]+dp[i+][y];//cout<<s[x][i]<<" "<<s[i+1][y]<<" "<<" "<<i+1<<" "<<y<<" "<<dp[x][y]<<endl;
}
if(dp[x][y]<=k&&!flag) continue;
if(dp[x][y]>=k)
{
dp[x][y] = k;
if(!flag)
{
int g = ,q=;
int k1 = strlen(s[x][i]);
if(o[x][i]<=&&k1==)
{
for(j = ; j <= o[x][i]; j++)
s[x][y][g++] = s[x][i][];
}
else if(o[x][i]<=&&k1==)
{
for(j = ; j <= o[x][i] ; j++)
{
s[x][y][g++] = s[x][i][];
s[x][y][g++] = s[x][i][];
}
}
else
{
if(o[x][i]>)
{
int qq = o[x][i],a[],t=;
while(qq)
{
a[t++] = qq%;
qq/=;
}
for(j = t- ; j >= ; j--)
s[x][y][g++] = a[j]+'';
s[x][y][g++] = '(';
q = ;
}
for(j = ; j < k1;j++)
s[x][y][g++] = s[x][i][j];
if(q)
s[x][y][g++] = ')';
}
k1 = strlen(s[i+][y]);q=;
if(o[i+][y]<=&&k1==)
{
for(j = ; j <= o[i+][y]; j++)
s[x][y][g++] = s[i+][y][];
}
else if(o[i+][y]<=&&k1==)
{
for(j = ; j <= o[i+][y] ; j++)
{
s[x][y][g++] = s[i+][y][];
s[x][y][g++] = s[i+][y][];
}
}
else
{
if(o[i+][y]>)
{
int qq = o[i+][y],a[],t=;
while(qq)
{
a[t++] = qq%;
qq/=;
}
for(j = t- ; j >= ; j--)
s[x][y][g++] = a[j]+'';
s[x][y][g++] = '(';
q = ;
}
for(j = ; j < k1;j++)
s[x][y][g++] = s[i+][y][j];
if(q)
s[x][y][g++] = ')';
}
o[x][y] = ;
s[x][y][g] = '\0';
}
else
{
strcpy(s[x][y],s[x][i]);
//cout<<s[x][y]<<" "<<x<<" "<<y<<endl;
o[x][y] = o[x][i]+o[i+][y];
}
}
}
return dp[x][y];
}
int main()
{
int i,j,k;
for(i = ;i <= ; i++)
for(j = ;j <= ; j++)
dp[i][j] = INF;
cin>>ss;
k = strlen(ss);
int minz = dfs(,k-),q=strlen(s[][k-]);
//cout<<q<<endl;
if(o[][k-]<=&&q==)
for(i = ; i <= o[][k-] ; i++)
cout<<s[][k-][];
else if(o[][k-]<=&&q==)
for(i = ; i <= o[][k-] ; i++)
{
cout<<s[][k-][]<<s[][k-][];
}
else
{
if(o[][k-]>)
cout<<o[][k-]<<'(';
for(i = ; i < q ; i++)
cout<<s[][k-][i];
if(o[][k-]>)
cout<<')';
}
puts("");
return ;
}
ural1238. Folding(记忆化)的更多相关文章
- *HDU1142 最短路+记忆化dfs
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- [ACM_动态规划] 数字三角形(数塔)_递推_记忆化搜索
1.直接用递归函数计算状态转移方程,效率十分低下,可以考虑用递推方法,其实就是“正着推导,逆着计算” #include<iostream> #include<algorithm> ...
- 【BZOJ-3895】取石子 记忆化搜索 + 博弈
3895: 取石子 Time Limit: 1 Sec Memory Limit: 512 MBSubmit: 263 Solved: 127[Submit][Status][Discuss] D ...
- hdu3555 Bomb (记忆化搜索 数位DP)
http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others) Memory ...
- Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)
题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...
- hdu 4826(dp + 记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4826 思路:dp[x][y][d]表示从方向到达点(x,y)所能得到的最大值,然后就是记忆化了. #i ...
- zoj 3644(dp + 记忆化搜索)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 思路:dp[i][j]表示当前节点在i,分数为j的路径条数,从 ...
- loj 1044(dp+记忆化搜索)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26764 思路:dp[pos]表示0-pos这段字符串最少分割的回文 ...
- DP(记忆化搜索) + AC自动机 LA 4126 Password Suspects
题目传送门 题意:训练指南P250 分析:DFS记忆化搜索,范围或者说是图是已知的字串构成的自动机图,那么用 | (1 << i)表示包含第i个字串,如果长度为len,且st == (1 ...
随机推荐
- POJ 1065 Wooden Sticks
Wooden Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16262 Accepted: 6748 Descri ...
- PHP PDO_MYSQL 操作类 YAF嵌入高性能类而准备
https://github.com/indieteq/PHP-MySQL-PDO-Database-Class PHP-PDO-MySQL-Class A PHP MySQL PDO class s ...
- HDU 4287 Intelligent IME(string,map,stl,make_pair)
题目 转载来的,有些stl和string的函数蛮好的: //numx[i]=string(sx); //把char[]类型转换成string类型 // mat.insert(make_pair(num ...
- POJ 2028
#include <iostream> #define MAXN 200 using namespace std; int mark[MAXN]; int main() { //freop ...
- Session、Cookie及cache的区别
Session 是单用户的会话状态.当用户访问网站时,产生一个 sessionid.并存在于 cookies中.每次向服务器请求时,发送这个 cookies,再从服务器中检索是否有这个 session ...
- copy
拷贝文件,不覆盖重复文件 yes no|cp -i a b
- lintcode:两数组的交 II
题目 计算两个数组的交 注意事项 每个元素出现次数得和在数组里一样答案可以以任意顺序给出 样例 nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2, 2]. 解题 ...
- JavaPersistenceWithHibernate第二版笔记-第四章-Mapping persistent classes-002identity详解
一.简介 1.You now have three methods for distinguishing references: Objects are identical if they occ ...
- Android笔记——四大组件详解与总结
android四大组件分别为activity.service.content provider.broadcast receiver. ------------------------------- ...
- Mysql笔记——DDL
数据库模式定义语言DDL(Data Definition Language),是用于描述数据库中要存储的现实世界实体的语言.一个数据库模式包含该数据库中所有实体的描述定义. =========== ...