码迷,mamicode.com
首页 > 其他好文 > 详细

uva 10026 Problem C: Edit Step Ladders

时间:2014-10-13 00:13:08      阅读:321      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   sp   

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

 通过对每一个字符串,每一个位置进行枚举三个操作,然后二分查找操作后的字符串是否存在,dp记录。

bubuko.com,布布扣
  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 #define N 25000
  5 using namespace std;
  6 
  7 char g[N][30];
  8 char s[30];
  9 int dp[N*10];
 10 
 11 void change(char *g,char *s,int pos,char ch)
 12 {
 13     int k=strlen(g);
 14     for(int i=0; i<k; i++)
 15     {
 16         s[i]=g[i];
 17     }
 18     s[pos]=ch;
 19     s[k]=\0;
 20 }
 21 
 22 void add(char *g,char *s,int pos,char ch)
 23 {
 24     int k=strlen(g);
 25     for(int i=0; i<pos; i++)
 26     {
 27         s[i]=g[i];
 28     }
 29     s[pos]=ch;
 30     for(int j=pos; j<k; j++)
 31     {
 32         s[j+1]=g[j];
 33     }
 34     s[k+1]=\0;
 35 }
 36 
 37 void del(char *g,char *s,int pos)
 38 {
 39     int k=strlen(g);
 40     for(int i=0; i<pos; i++)
 41     {
 42         s[i]=g[i];
 43     }
 44     for(int j=pos+1; j<k; j++)
 45     {
 46         s[j-1]=g[j];
 47     }
 48     s[k-1]=\0;
 49 }
 50 
 51 
 52 void Get_change(char *g,char *s,char ch,int pos,int f)
 53 {
 54     if(f==1) change(g,s,pos,ch);
 55     else if(f==2) add(g,s,pos,ch);
 56     else if(f==3) del(g,s,pos);
 57 }
 58 
 59 int bs(char *s,int r)
 60 {
 61     r--;
 62     int l=0;
 63     int mid;
 64     while(l<=r)
 65     {
 66         mid=(l+r)>>1;
 67         if(strcmp(g[mid],s)==0)
 68         {
 69             return mid;
 70         }
 71         else if(strcmp(g[mid],s)<0)
 72         {
 73             l=mid+1;
 74         }
 75         else
 76         r=mid-1;
 77     }
 78     return -1;
 79 }
 80 int main()
 81 {
 82     //freopen("1.txt","r",stdin);
 83     //freopen("2.txt","w",stdout);
 84     int cnt=0;
 85     while(gets(g[cnt]))
 86     {
 87         cnt++;
 88     }
 89     int ans=0;
 90     for(int i=0; i<cnt; i++)
 91     {
 92         dp[i]=1;
 93         for(int k=1; k<=3; k++)
 94         {
 95             for(int j=0; j<(int)strlen(g[i]); j++)
 96             {
 97                 for(int c=0; c<26; c++)
 98                 {
 99                     Get_change(g[i],s,a+c,j,k);
100                     if(strcmp(g[i],s)<0) break;
101                     int x=bs(s,i);
102                     if(x>=0) dp[i]=max(dp[i],dp[x]+1);
103                 }
104             }
105 
106         }
107         ans=max(ans,dp[i]);
108     }
109     printf("%d\n",ans);
110     return 0;
111 }
View Code

 

uva 10026 Problem C: Edit Step Ladders

标签:style   blog   http   color   io   os   ar   for   sp   

原文地址:http://www.cnblogs.com/fanminghui/p/4021195.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!