Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

(附代码 )谁来帮我 帮我 帮我 帮我 帮我!

Posted by nuciedh at 2007-07-23 21:36:28 on Problem 3267
In Reply To:超时!  该怎么做  ? Posted by:nuciedh at 2007-07-23 19:59:03
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int w, l, d[301], m[301][301];
char p[301], s[601][27];

int match (int l, int r, int k)
{
    int i, key, tmp;
    if (l > r) return 0;
    for (i = 0, key = 0; l + i <= r; i ++)
    if (s[k][key] == p[l + i]) key ++;
    if (key == strlen (s[k])) return key;
    else                      return 0;
}

void print ()
{
    for (int i = 0; i < l; i ++)
    printf ("%d ", d[i]);
    printf ("\n");
}

void solve ()
{
    int i, j, k, mat, tmp;
    scanf ("%s", p);
    for (i = 0; i < w; i ++)
    scanf ("%s", s[i]);
    for (i = 0; i < l; i ++) d[i] = i + 1;
    
    memset (m, 0, sizeof (m));
    for (i = 0; i < l; i ++)
    for (j = 0; j <= i; j ++)
    {
        for (k = 0; k < w; k ++)
        {
            if (s[k][0] == p[j])
            {
                int key = match (j, i, k);
                if (key > m[j][i]) m[j][i] = key;
            }
        }
    }
    for (i = 0; i < l; i ++)
    {
        for (j = 0; j <= i; j ++)
        {
            mat = m[j][i];
            if (j && mat)
            {
                mat = d[j - 1] + (i - j + 1 - mat);
                if (d[i] > mat) d[i] = mat;
            }
            else
            {
                if (i + 1 - mat < d[i]) d[i] = i + 1 - mat;
            }
        }
    }
    printf ("%d\n", d[l - 1]);
}

int main ()
{
    while (EOF != scanf ("%d %d", &w, &l))
    solve ();
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator