题目链接: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. ios7 苹果原生二维码扫描(和微信类似)

    在ios7苹果推出了二维码扫描,以前想要做二维码扫描,只能通过第三方ZBar与ZXing. ZBar在扫描的灵敏度上,和内存的使用上相对于ZXing上都是较优的,但是对于 “圆角二维码” 的扫描确很困 ...

  2. (正则表达式应用) 替换自闭合标签(self-closing tag)的method

    var str = "<sup><div class=\"he's\"/></sup><span id=\"cs\&q ...

  3. border-radius的浏览器兼容

    早期开发时,圆角的实现对于开发者来说是一个头疼的事情,因为需要添加多种图片以及定位去实现固定.缺乏灵活性的圆角容器,故css3中引入了border-radius属性,使得开发者实现圆角变得轻而易举! ...

  4. Lucene5.x 中文 同义词

    查询好好多资料,英文同义词好好的,中文就不行,多谢网友支持,拼接了好多代码,然后修改了一些,不足之处,多谢指正. 直接上代码吧,在代码中了解怎么分词的最好 1,创建分词引擎 public interf ...

  5. SGU 246. Black & White(数论)

    题意: 有2*n-1个黑色和白色的珠子组成的环形项链,求至少需要多少颗黑色珠子才能使任意排列的项链中都存在两个黑珠间有n个珠子. (2*n-1<=2^31-1); Solution: 先分析n= ...

  6. ACM hdu 1019 Least Common Multiple

    Problem Description The least common multiple (LCM) of a set of positive integers is the smallest po ...

  7. javascript 中的数据驱动页面模式

    前段时间一直在想前端MVC的意义.这个话题仁者见仁,但是MVC的使用方法给我提了一个管理数据的有意思的想法--数据管理和数据驱动页面.我们以前的思路一直是事件驱动页面,事件驱动页面合乎逻辑而且节约代码 ...

  8. HTML5 程序设计笔记(二)

    Canvas API 1.HTML5 Canvas 概述 1.1 历史 Canvas的概念最初是由苹果公司提出的,用于在Mac OS X WebKit中创建控制板部件(dashboard widget ...

  9. windows 安装maven 环境

    1.maven 下载地址: http://maven.apache.org/index.html 2.解压到目录并配置环境变量 M2_HOME   D:\maven\maven path       ...

  10. js快速排序法

    var quickSort = function(arr) { if (arr.length <= 1) { return arr; } var pivotIndex = Math.floor( ...