| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
给我提示了~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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator