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

my code

Posted by luckyyang at 2009-06-08 19:31:27 on Problem 1141
In Reply To:Re:那位帮忙看一下,小的测试数据都是对的,为什么已提交就是RunTime Error呢 Posted by:luckyyang at 2009-06-08 19:30:38
//保持下标一致,必须使得s[k-1]的形式
#include <iostream>
#include<string>
using namespace std;
const int inf=30000;
const int Max=100;
const int front=-1;
const int back=-2;
const int none=0;
void solve(int pos[Max+1][Max+1],int i,int j,string s,char symbol[Max+1][Max+1]);
int main()
{
    int n, j;string s;
    int d[Max+1][Max+1],pos[Max+1][Max+1];
    char symbol[Max+1][Max+1];
    memset(&d,0,sizeof(int)*(Max+1)*(Max+1));
    memset(&pos,0,sizeof(int)*(Max+1)*(Max+1));
    memset(&symbol,0,sizeof(int)*(Max+1)*(Max+1));
    cin>>s;
    n=(int)s.length();
    for(int k=1;k<=n;k++)
    {
        d[k][k]=1;
        if(s[k-1]=='(')
        {
            pos[k][k]=back;//在后面插入字符
            symbol[k][k]=')';
        }
        else if(s[k-1]==')')
        {
            pos[k][k]=front;//在前面插入字符
            symbol[k][k]='(';
        }
        else if(s[k-1]=='[')
        {
            pos[k][k]=back;//后插
            symbol[k][k]=']';
        }
        else
        {
            pos[k][k]=front;//前插
            symbol[k][k]='[';
        }
    }
    for(int k=1;k<=n-1;k++)//阶段
        for(int i=1;i<=n-k;i++)//i,i+k<=n
        {
            j=i+k;
            d[i][j]=inf;
            if((s[i-1]=='(' && s[j-1]==')')||(s[i-1]=='[' && s[j-1]==']'))//s下标从0开始的
            {
                if(d[i][j]>d[i+1][j-1])
                {
                    d[i][j]=d[i+1][j-1];
                    pos[i][j]=none;//不用插入
                    symbol[i][j]='\0';
                }
            }
            if(s[i-1]=='('|| s[i-1]=='[')
            {
                if(d[i][j]>d[i+1][j]+1)
                {
                    d[i][j]=d[i+1][j]+1;
                    pos[i][j]=back;//后插
                    if(s[i-1]=='(')
                        symbol[i][j]=')';
                    else
                        symbol[i][j]=']';
                }
            }
            if(s[j-1]==')' ||s[j-1]==']')
            {
                if(d[i][j]>d[i][j-1]+1)
                {
                    d[i][j]=d[i][j-1]+1;
                    pos[i][j]=front;
                    if(s[j-1]==')')
                        symbol[i][j]='(';
                    else
                        symbol[i][j]='[';
                }
            }
            for(int r=i;r<=i+k-1;r++)
            {
                if(d[i][j]>d[i][r]+d[r+1][i+k])
                {
                    d[i][j]=d[i][r]+d[r+1][i+k];
                    pos[i][j]=r;//
                }
            }
    }
    solve(pos,1,n,s,symbol);
    cout<<endl;
    return 0;
}
void solve(int pos[Max+1][Max+1],int i,int j,string s,char symbol[Max+1][Max+1])
{
    //注意insert函数在索引之前插入值
    if(i>j)return;
    if(pos[i][j]==none)
    {
        cout<<s[i-1];
        solve(pos,i+1,j-1,s,symbol);
        cout<<s[j-1];
    }
    if(pos[i][j]==back)
    {
        cout<<s[i-1];
        solve(pos,i+1,j,s,symbol);
        cout<<symbol[i][j];
    }
    if(pos[i][j]==front)
    {
        cout<<symbol[i][j];
        solve(pos,i,j-1,s,symbol);
        cout<<s[j-1];
    }
    if(pos[i][j]>=i && pos[i][j]<=j)
    {
        solve(pos,i,pos[i][j],s,symbol);
        solve(pos,pos[i][j]+1,j,s,symbol);
    }
}

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