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 ulinux at 2011-10-03 15:56:06 on Problem 1056
In Reply To:排序之,证明如果一个串a是串b的前缀,那么串a一定在串b前面,并且a,b之间的串都是b的前缀 Posted by:stupidjohn at 2011-02-04 13:41:10
我的想法是先用sort排序,然后前一个和后一个直接比较,即可得到结果。贴代码:

#include <iostream>
#include <iomanip>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define ZERO 10E-6
#define PI 3.1415926

int main()
{
    int num=0;
    int count=1;
    int i;
    string str[2000],end;
    bool flag;

    while(cin>>end)
    {
        if(end=="9")
        {
            sort(str,str+num,less<string>());
            //for(i=0;i<num;i++)
            //    cout<<str[i]<<endl;
            for(i=1,flag=true;i<num&&flag;i++)
            {
                if(str[i-1]==str[i].substr(0,str[i-1].length()))
                    flag=false;
            }
            if(flag)
                cout<<"Set "<<count++<<" is immediately decodable"<<endl;
            else
                cout<<"Set "<<count++<<" is not immediately decodable"<<endl;
            num=0;
        }
        else
        {
            str[num++]=end;
        }
    }
    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