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 ...
随机推荐
- iOS8上本地通知接收不到的问题
需要手动加上这句话 if ([UIApplicationinstancesRespondToSelector:@selector(registerUserNotificationSettings:) ...
- JavaScript中的作用域和闭包
首先强烈安利<你不知道的JavaScript>,JS初学者进阶必读. 对于从C++.Java等静态语言转向JavaScript的初学者(比如我)来说,JS一些与众不同而又十分要紧的特性使得 ...
- ENC28J60 + M430G2553,用uip搭建http服务器,解决“在XP系统下可以访问,在Win7下不能访问”的问题
近日,用ENC28J60,在M430G2553上搭建一个简单的HTTP服务器,结果发现在XP系统下可以访问,在Win7下不能访问,非常奇葩的问题. 通过抓包,如下图,计算机(IP地址为192.168. ...
- PHP中的设计模式:单例模式(译)
原文链接:http://coderoncode.com/2014/01/27/design-patterns-php-singletons.html 单例模式用于限制类实例化到单个对象,当整个系统只需 ...
- QTableWidget中添加按钮
添加按钮 void QTableWidget::setCellWidget ( int row, int column, QWidget * widget ) widget可以是自己定义的按钮 cla ...
- css 文本两端对齐
在做表单时我们经常遇到让上下两个字段对齐的情况,比如姓名, 手机号码, 出生地.这样我们就要用到 text-align, text-justify样式了. text-align直接设为justify就 ...
- html5+css3实现上拉和下拉刷新
<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...
- C# Delegate 异步调用
namespace ConsoleApplication22 { /// /// 异步操作 /// /// /// /// //internal Func<int,int,int> int ...
- css:中文词不断开,整体换行
一.问题 关于文字的换行与不换行的问题有些特殊情况,是使用css的word-break等属性实现不了的,下面的情况就证明了: 我们想要的效果是,一个词整体换行或不换行,“兼职测试”可以都换至第二行 ...
- 趣味C程序100.1 .1 绘制余弦曲线
说明:1.本问题来源于<C语言经典.趣味.实用程序设计编程百例精解>,所有程序为本人自己编写.与原程序不同之处作有标记. 2.本系列所有程序均使用codeblocks编译,操作系统为Win ...