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

any fantastic skill? My prog got TLE.

Posted by alpc06 at 2005-12-06 13:29:07 on Problem 1159
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;
    
int main()
{
    int n;
    int i;
    string x,y;
    
    while ( cin >> n)
    {
        cin >> x;
        y = x;
        int len = x.length();
        
        for (i = 0; i <= (len-1)/2; i++)
        {
            int tmp = y[i];
            y[i] = y[len-1-i];
            y[len-1-i] = tmp;
        }     
        
        int m = x.length(), l = y.length();
        short c[2][5001];
        int i,j;
    
   	for (j = 0; j <= l; j++)
   		c[0][j] = 0;
   	c[1][0] = 0;
   		
    for (i = 1; i <= m; i++)
    	for (j = 1; j <= l; j++)
    	{
    	    if (x[i-1] == y[j-1])
    	    	c[i % 2][j] = c[(i+1) % 2][j-1]+1;
   	    	else
   	    		if (c[i % 2][j-1] >= c[(i+1) % 2][j])
   	    			c[i % 2][j] = c[i % 2][j-1];
  			    else
  			    	c[i % 2][j] = c[(i+1) % 2][j];
    	}    
        
        cout << n - c[m % 2][l] << endl;
        
    }    
    
    system("pause");
    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