uva 10026 Problem C: Edit Step Ladders

來源:互聯網
上載者:User

標籤: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記錄。

  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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.