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 ...
随机推荐
- 关于Androdi中SQLITE 3采用GBK编码存储,数据库中文乱码问题。
1.最近开发一个项目,用SQLite Expert Personal打开数据库如下图,title会产生乱码,问题. 2.由于SQL lite默认是存储UTF-8格式,后来更改数据库编码类型为ANSI, ...
- C#一些小技巧(二)
教你们怎么改配色方案,因为本人智障了很久,每次想改颜色的时候都会看到一大圈的选项,难以琢磨,但是智障了那么久终于被我找到了所有的关联. 首先,要告诉你们的是,其实C#里面要改的东西只有那么几个,但是注 ...
- [C#][转]与MySql连接访问
using System;using System.Configuration;using MySql.Data.MySqlClient;/// <summary>/// TestDate ...
- C++专题 - Qt是什么
Qt是一个1991年由奇趣科技开发的跨平台C++图形用户界面应用程序开发框架.它既可以开发GUI程式,也可用于开发非GUI程式,比如控制台工具和服务器.Qt是面向对象的框架,使用特殊的代码生成扩展(称 ...
- ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印)
Greatest Common Increasing Subsequence 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...
- Observer 模式
Observer模式要解决的问题为:建立一个一(Subject)对多(Observer)的依赖关系,并且做到当“一”变化的时候,依赖这个“一”的多也能够同步改变.最常见的一个例子就是:对同一组数据进行 ...
- 如何给html元素的onclick事件传递参数(即如何获取html标签的data-*属性)
现在做的一个小系统为了达到领导所说的很炫的效果有用到Metro UI CSS,但是因为如何给每个磁贴(div标签)的click事件传递参数折腾了蛮久(偶是菜鸟),后来终于找到一个解决方案即通过data ...
- Ubuntu 之旅 —— 解决sudo: source: command not found错误
$ sudo -s # source /etc/profile
- Core MVC
Core MVC 配置全局路由前缀 前言 大家好,今天给大家介绍一个 ASP.NET Core MVC 的一个新特性,给全局路由添加统一前缀.严格说其实不算是新特性,不过是Core MVC特有的. 应 ...
- BASLER 镜头选型白皮书
本文翻译自Basler镜头选型白皮书 有许多方法来进行镜头选型.本文将会讨论其中的指导原则,以帮助你在项目中选择合适的镜头.我们将讨论许多镜头的基本概念,比如镜头接口.图像大小.放大率.焦距.F数和光 ...