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

纠结,对string用sort排序,总是超时呢?只好改成数组了

Posted by ulinux at 2011-10-03 21:04:29 on Problem 3630
#include <iostream>
#include <iomanip>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define ZERO 10E-6
#define PI 3.1415926

int cmp(const void *a,const void *b)
{
    return strcmp((char*)a,(char*)b);
}

int main()
{
    char str[10101][12];
    int n,m,i,len;
    bool flag;

    cin>>n;
    while(n--)
    {
        cin>>m;
        for(i=0;i<m;i++)
            cin>>str[i];
        qsort(str,m,sizeof(str[0]),cmp);
        for(i=1,flag=true;i<m&&flag;i++)
        {
            len=strlen(str[i-1])<=strlen(str[i])?strlen(str[i-1]):strlen(str[i]);
            if(strncmp(str[i-1],str[i],len)==0)
                flag=false;
        }
        flag?cout<<"YES"<<endl:cout<<"NO"<<endl;
    }
    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