动态规划之Edit Step Ladders
来源:互联网 发布:哪里可以买到淘宝号 编辑:程序博客网 时间:2024/06/17 15:07
题目:
An edit step is a transformation from one word x to another word y such that x and y are words in the dictionary, and x can be transformed to y by adding, deleting, or changing one letter. So the transformation from dig to dog or from dog to do are both edit steps. An edit step ladder is a lexicographically ordered sequence of words w1,w2,...,wn such that the transformation from wi to wi+1 is an edit step for all i from 1 to n−1. For a given dictionary, you are to compute the length of the longest edit step ladder.
Input
The input to your program consists of the dictionary – a set of lower case words in lexicographic order – one per line. No word exceeds 16 letters and there are no more than 25000 words in the dictionary.
Output
The output consists of a single integer, the number of words in the longest edit step ladder.
Sample Input
cat
dig
dog
fig
fin
fine
fog
log
wine
Sample Output
5
动态规划很好想,就是在判断的时候先把一个词所有可能的变换找出来,然后由于单词有序可以二分查找,否则一个一个去比会超时...这道题因为变换的时候末尾没加\0,wa了超多次..郁闷死我了....大致思路是这样,其中还有一些点应该还可以优化....啊我去,先不管了...
#include<stdio.h>#include<string.h>char word[25000][20];int dp[25000];int find(int r,char target[]){ int l,mid,flag; l=0; while(l<=r){ mid=(l+r)/2; flag=strcmp(word[mid],target); if(flag==0) return mid; else if(flag>0) r=mid-1; else l=mid+1; } return -1;}int change(int pos,char t[],int i,char c){ strcpy(t,word[i]); t[pos]=c; return find(i-1,t);}int del(int pos,char t[],int i){ int k,j=0; for(k=0;k<strlen(word[i]);k++){ if(pos==k) continue; t[j]=word[i][k]; j++; } t[j]='\0'; return find(i-1,t);}int add(int pos,char t[],int j,char c){ int i; strcpy(t,word[j]); t[pos]=c; for(i=pos;i<strlen(word[j]);i++) t[i+1]=word[j][i]; t[strlen(word[j])+1]='\0'; return find(j-1,t);}int main(){ int n=0; int i,pos,index,tmp,ans; char c; char t[30]; while(scanf("%s",word[n])!=EOF) n++; dp[0]=ans=1; for(i=1;i<n;i++){ tmp=0; for(c='a';c<'a'+26;c++){ for(pos=0;pos<=strlen(word[i]);pos++){ index=add(pos,t,i,c); if(index>=0&&dp[index]>tmp) tmp=dp[index]; } for(pos=0;pos<strlen(word[i]);pos++){ index=change(pos,t,i,c); if(index>=0&&dp[index]>tmp) tmp=dp[index]; } } for(pos=0;pos<strlen(word[i]);pos++){ index=del(pos,t,i); if(index>=0&&dp[index]>tmp) tmp=dp[index]; } dp[i]=tmp+1; if(dp[i]>ans) ans=dp[i]; } printf("%d\n",ans); return 0;}
- 动态规划之Edit Step Ladders
- ZJU1876 Edit Step Ladders - 动态规划 最长序列+二分优化
- 10029 - Edit Step Ladders
- 10029 - Edit Step Ladders
- UVA10029- Edit Step Ladders
- PKU 2564 Edit Step Ladders
- uva 10029 Edit Step Ladders
- UVA 10029 Edit Step Ladders
- UVA 10029 - Edit Step Ladders
- POJ 2564 Edit Step Ladders
- POJ - 2564 Edit Step Ladders
- POJ - 2564 Edit Step Ladders
- UVA 10029 - Edit Step Ladders
- uva 10029 - Edit Step Ladders
- UVA - 10029 Edit Step Ladders
- UVA - 10029 Edit Step Ladders
- UVa 10029 - Edit Step Ladders
- uva 10029 Edit Step Ladders
- seo学习1
- Kafka安装部署
- 组合数学--卡特兰数
- hdu4292Food【拆点网络流】
- Android 中进程、线程的概念
- 动态规划之Edit Step Ladders
- 数据结构(一)-----线性表
- NetWorkingTool
- 进程与线程(一)
- eXtremeDB Performance tuning
- 数据持久化
- 蓝桥杯省赛c++b组.1
- [bzoj3513][MUTC2013]idiots
- SavaTool_归档