UVa 10029 hash + dp
第一次正式用hash表。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; const int maxn = ;
const int HASH = ; char s[maxn][];
int head[HASH],next[maxn];
int dp[maxn];
int n;
char temp[]; //变出来的字符串 int hash(char *s)
{
int seed = ;
int ret = ;
while(*s)
{
ret = ret * seed + *(s++);
}
return (ret & 0x7fffffff ) % HASH ;
} void input()
{
n = ;
memset(head,-,sizeof(head)); while(scanf("%s",s[n]) == )
{
int id = hash(s[n]);
next[n] = head[id];
head[id] = n;
n++;
}
n--;
} int search()
{
int i;
int id = hash(temp);
for(i=head[id]; i!=-; i=next[i])
{
if(strcmp(temp,s[i]) == )
break;
}
return i;
} void add(int cur,int loc,int addnum)
{
int len = strlen(s[cur]);
int i,j;
for(i=,j=; i<loc; i++)
{
temp[j++] = s[cur][i];
}
temp[j++] = 'a' + addnum;
for(; i<len; i++)
{
temp[j++] = s[cur][i];
}
temp[j] = '\0';
} void del(int cur,int loc)
{
int len = strlen(s[cur]);
int i,j;
for(i=,j=; i<loc; i++)
temp[j++] = s[cur][i];
i++;
for(; i<len; i++)
temp[j++] = s[cur][i];
temp[j] = '\0';
} void change(int cur,int loc,int addnum)
{
strcpy(temp,s[cur]);
temp[loc] += addnum;
} int dfs(int cur)
{
if(dp[cur] != -) return dp[cur]; int len = strlen(s[cur]);
int ret = ; //add;
for(int i=; i<=len; i++)
{
for(int j=; j<; j++)
{
add(cur,i,j);
int id = search();
if(id!=- && strcmp(s[cur],temp) < )
{
ret = max(ret,dfs(id)+);
}
}
} //del;
for(int i=; i<len; i++)
{
del(cur,i);
int id = search();
if(id != - && strcmp(s[cur],temp) < )
ret = max(ret,dfs(id)+);
} //change;
for(int i=; i<len; i++)
{
int lim = 'z' - s[cur][i];
for(int j=; j<=lim; j++)
{
change(cur,i,j);
int id = search();
if(id != - && strcmp(s[cur],temp) < )
ret = max(ret,dfs(id)+);
}
}
dp[cur] = ret;
return ret;
} void solve()
{
memset(dp,-,sizeof(dp));
int ans = ;
for(int i=; i<=n; i++)
{
ans = max(ans,dfs(i));
}
printf("%d\n",ans+);
}
int main()
{
// freopen("E:\\acm\\input.txt","r",stdin);
input();
solve();
}
UVa 10029 hash + dp的更多相关文章
- UVA.10192 Vacation (DP LCS)
UVA.10192 Vacation (DP LCS) 题意分析 某人要指定旅游路线,父母分别给出了一系列城市的旅游顺序,求满足父母建议的最大的城市数量是多少. 对于父母的建议分别作为2个子串,对其做 ...
- UVA.10130 SuperSale (DP 01背包)
UVA.10130 SuperSale (DP 01背包) 题意分析 现在有一家人去超市购物.每个人都有所能携带的重量上限.超市中的每个商品有其相应的价值和重量,并且有规定,每人每种商品最多购买一个. ...
- Edit Step Ladders - UVA 10029
题意 题目链接(Virtual Judge):Edit Step Ladders - UVA 10029 题意: 如果单词 \(x\) 能通过添加.删除或修改一个字母变换为单词 \(y\),则称单词 ...
- UVA - 10029 Edit Step Ladders (二分+hash)
Description Problem C: Edit Step Ladders An edit step is a transformation from one word x to another ...
- BZOJ 1260&UVa 4394 区间DP
题意: 给一段字符串成段染色,问染成目标串最少次数. SOL: 区间DP... DP[i][j]表示从i染到j最小代价 转移:dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k ...
- bzoj 3197 [Sdoi2013]assassin(Hash+DP+KM)
Description Input Output Sample Input 4 1 2 2 3 3 4 0 0 1 1 1 0 0 0 Sample Output 1 HINT [思路] Hash,D ...
- uva 10154 贪心+dp
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA 1358 - Generator(dp+高斯消元+KMP)
UVA 1358 - Generator option=com_onlinejudge&Itemid=8&page=show_problem&category=524& ...
- uva 1534 - Taekwondo(dp+馋)
题目连接:uva 1534 - Taekwondo 题目大意:有两组什么东西,题目背景有点忘记了,就是给出两组数,两组个数分别为n,m,要求找出min(n,m)对数.每一个数最多最多选一次,使得这mi ...
随机推荐
- Oracle数据库学习 视图、序列及存储过程
视图(View) 视图也被称作虚表,也就是虚拟的表,是一组数据的逻辑表示. 视图对应一个select语句,结果集被赋予一个名字,也就是视图的名字. 视图本身不包含任何数据,它只是包含映射到基表的一个查 ...
- AngularJS cookie的使用
Javascript使用document.cookie接口来处理简单的文本cookie,但现在大多数现代浏览器都可以使用html5 API了(sessionstorage和localstorage), ...
- jquery学习之旅
在jQuery中,css()方法的功能是设置或获取元素的某项样式属性. $<"div">.css("font-weight","bold& ...
- sass用法
可能刚开始我们学习前端的时候都习惯用html+css.来做网页,但是我们发现css有很多重复的代码或者是你要改里面的图片或者文字还有去诶个的找很麻烦,所以我们就用sass来简化它. 首先我们需要安装一 ...
- 关于python decode()和 encode()
1.先收集一下这几天看到的关于decode()解码和encode()编码的用法 bytes和str是字节包和字符串,python3中会区分bytes和str,不会混用这两个.字符串可以编码成字节包,而 ...
- SNN--Second Name Node
NameNode有这么几个配置: Property Description Suggested value dfs.name.dir Directory in NameNode’s local fil ...
- 去除UINavigationBar默认透明度的方法
UINavigationbar的属性translucent,用来控制导航条的透明度的: iOS7+版本后,navigationbar的translucent属性默认为YES,及默认带有透明度 [sel ...
- Python connect zookeeper use the kazoo module
doc:http://kazoo.readthedocs.org/en/latest/basic_usage.html eg: from kazoo.client import KazooClient ...
- Life Forms
poj3294:http://poj.org/problem?id=3294 题意:就是求n个串的中一个最大的子串,这个子串在超过n/2的串中出现. 题解:这是一道好题.首先一种解法就是用后缀数组来搞 ...
- 一个QT 3D转动控件
其实说到底就是不停的截图,做出的幻觉.联想起360拖动图片,也是合并图片做出的效果,可见的对GUI来说图片是一切,是最根本的解决一切问题的办法,编程仅是辅助实现手段而已,我要记住这一点. .h文件 # ...