题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=970

第一次正式用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的更多相关文章

  1. UVA.10192 Vacation (DP LCS)

    UVA.10192 Vacation (DP LCS) 题意分析 某人要指定旅游路线,父母分别给出了一系列城市的旅游顺序,求满足父母建议的最大的城市数量是多少. 对于父母的建议分别作为2个子串,对其做 ...

  2. UVA.10130 SuperSale (DP 01背包)

    UVA.10130 SuperSale (DP 01背包) 题意分析 现在有一家人去超市购物.每个人都有所能携带的重量上限.超市中的每个商品有其相应的价值和重量,并且有规定,每人每种商品最多购买一个. ...

  3. Edit Step Ladders - UVA 10029

    题意 题目链接(Virtual Judge):Edit Step Ladders - UVA 10029 题意: 如果单词 \(x\) 能通过添加.删除或修改一个字母变换为单词 \(y\),则称单词 ...

  4. UVA - 10029 Edit Step Ladders (二分+hash)

    Description Problem C: Edit Step Ladders An edit step is a transformation from one word x to another ...

  5. 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 ...

  6. 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 ...

  7. uva 10154 贪心+dp

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  8. UVA 1358 - Generator(dp+高斯消元+KMP)

    UVA 1358 - Generator option=com_onlinejudge&Itemid=8&page=show_problem&category=524& ...

  9. uva 1534 - Taekwondo(dp+馋)

    题目连接:uva 1534 - Taekwondo 题目大意:有两组什么东西,题目背景有点忘记了,就是给出两组数,两组个数分别为n,m,要求找出min(n,m)对数.每一个数最多最多选一次,使得这mi ...

随机推荐

  1. Java验证码代码

    public class VerifyCodeController { private int width = 90;//定义图片的width private int height = 20;//定义 ...

  2. js原型解析

    我们都知道javascript因为具有了继承以及变量等等一系列的特性之后才被人们认为具有一门编程语言的资格,在后续的不断发展中,js在原生的基础上扩展了基于jquery等等的库,甚至衍生了像node. ...

  3. pc110301QWERTYU

    水题一道,SOLVED只是次数的问题.map一下,就是很easy啦. #include<iostream> #include<cstdio> #include<cstri ...

  4. [Neural Networks] Momentum

    一.目的 加快参数的收敛速度. 二.做法 另第t次的权重更新对第t+1次的权重更新造成影响. 从上式可看出,加入momentum后能够保持权重的更新方向,同时加快收敛.通常alpha的取值为[0.7, ...

  5. php模拟POST请求提交数据

    php模拟POST请求提交数据 1.基于fsockopen function phppost00($jsonString){ $URL='https://www.jy.com/phppostok.ph ...

  6. GoogleCode新手教程

    GoogleCode页面介绍 Project Home 首先显示的是project home,页面左边的是这个项目的介绍,右边的License是说明使用的是什么开源协议,Labels是标签的意思,就是 ...

  7. VS2012编译生成XP可以执行的程序

    首先需要的就是下载VS2012的Update 4更新包,然后打开项目的属性页,在 配置属性->平台工具集 选项中选择 Visual Studio 2012 - Windows XP (v110_ ...

  8. bzoj 1005: [HNOI2008]明明的烦恼 prufer编号&&生成树计数

    1005: [HNOI2008]明明的烦恼 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2248  Solved: 898[Submit][Statu ...

  9. nodejs服务

    http://www.csser.com/board/4f55035305ee2e572400005e http://blog.fens.me/nodejs-server-forever/ http: ...

  10. 【 UVALive - 2197】Paint the Roads(上下界费用流)

    Description In a country there are n cities connected by m one way roads. You can paint any of these ...