Longest Prefix & Cow Pedigrees chapter 2.3 (dp)

longest prefix 

/*

ID: hubiao cave

PROG: prefix

LANG: C++

*/




#include<iostream>

#include<fstream>

#include<string>

using namespace std;

string sary[202],s;
bool g[200050];
int len[202],slen,ans;



int main()

{


    ifstream fin("prefix.in");

    ofstream fout("prefix.out");
    string str;
    int co=0;
    int i=0;
    while(fin>>str,str!=".")
    {
        ++co;
        sary[i]=str;
        len[i]=str.length();
        i++;
    }
    while(fin>>str)
    {
        s+=str;
    }
    slen=s.length();
    
    g[0]=true;
    for(int i=0;i<=slen;i++)
    {
        if(g[i])
        {
            ans=i;
            for(int j=0;j<co;j++)
            {
                if(i+sary[j].length()<=slen)
                for(int k=0;k<sary[j].length();k++)
                {
                    if(s[i+k]!=sary[j][k])
                        break;
                    else
                    {
                        if(k==sary[j].length()-1)
                        {
                            g[i+sary[j].length()]=1;
                        }
                    }

                }
            }
        }

    }
    fout<<ans<<endl;

    return 0;


}

cow pedigress

 1 /*
 2 
 3 ID: hubiao cave
 4 
 5 PROG: nocows
 6 
 7 LANG: C++
 8 
 9 */
10 
11 
12 
13 
14 #include<iostream>
15 
16 #include<fstream>
17 
18 #include<string>
19 #include<cstring>
20 using namespace std;
21 
22 
23 
24 int main()
25 
26 {
27 
28     ifstream fin("nocows.in");
29 
30     ofstream fout("nocows.out");
31     int N,K;
32     fin>>N>>K;
33     
34     int table[102][202]
35 ;
36     int smalltrees[102][202];
37 
38     for(int i=0;i<=101;i++)
39     {
40         memset(table[i],0,sizeof(int)*202);
41         memset(smalltrees[i],0,sizeof(int)*202);
42     }
43 
44     table[1][1]=1;
45     smalltrees[1][1]=1;
46     for(int i=2;i<=K;i++)
47     {
48         for(int j=1;j<=N;j++)
49         {
50             for(int k=1;k<=j;k++)
51             if(i==2)
52             {
53                 table[i][j]+=table[i-1][k]*table[i-1][j-k-1];
54                 smalltrees[i][j]=table[i][j]+smalltrees[i-1][j];
55                 //for(int m=1;m<i;m++)
56                     //smalltrees[i][j]+=smalltrees[m][j];
57             }
58             else
59             {
60                 if(i==4)
61                     i=4;
62                 table[i][j]+=table[i-1][k]*table[i-1][j-k-1];
63                 table[i][j]+=smalltrees[i-2][k]*table[i-1][j-k-1];
64                 table[i][j]+=table[i-1][k]*smalltrees[i-2][j-k-1];
65                 table[i][j]%=9901;
66                 smalltrees[i][j]=(table[i][j]+smalltrees[i-1][j])%9901;
67                 //for(int m=1;m<i;m++)
68                 //smalltrees[i][j]+=smalltrees[m][j];
69                 
70 
71             }
72         }
73     }
74 
75     fout<<table[K][N]%9901<<endl;
76     return 0;
77 
78 
79 }

 

posted @ 2013-09-08 19:30  cavehubiao  阅读(178)  评论(0编辑  收藏  举报