zoukankan      html  css  js  c++  java
  • uva 10026 Problem C: Edit Step Ladders

    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]='';
     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]='';
     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]='';
     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
    ",ans);
    110     return 0;
    111 }
    View Code
  • 相关阅读:
    第三章 对话拖延--摆脱拖延
    第二章 审问拖延:被架在审判席上的失败者恐惧症
    番茄学习法笔记以及初始尝试
    第一章 质疑拖延:是可恶的坏习惯还是应得的报应
    学习编程珠玑笔记记录-----第二章 算法
    李文业工作一年总结
    永远要努力向前
    怎样才能在工作中进步的更快
    windows下php扩展安装
    JS学习笔记
  • 原文地址:https://www.cnblogs.com/fanminghui/p/4021195.html
Copyright © 2011-2022 走看看