| ||||||||||
| 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 | |||||||||
注意排序。#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
struct Code{
Code(){zero = NULL, one= NULL, flag = false;};
Code* zero;
Code* one;
bool flag;
};
bool cmp(const string &a, const string &b){
if (a.size() < b.size() ) return true;
else return false;
}
int main()
{
Code root;
vector<string> input;
string s;
bool outputflag = false;
int count = 0;
while (cin >> s){
++count;
input.push_back(s);
while(cin >> s, s[0]!='9') input.push_back(s);
sort(input.begin(), input.end(), cmp);
for (vector<string>::iterator j = input.begin(); j != input.end(); ++j){
if (outputflag) break;
Code* p = &root;
for (vector<string>::value_type::iterator i = (*j).begin(); i != (*j).end(); ++i){
if (*i == '0'){
if (p->zero != NULL){
p = p->zero;
if (p->flag){
cout << "Set " << count << " is not immediately decodable" << endl;
outputflag = true;
break;
}
if (i+1 == (*j).end()){
p->flag = true;
}
}
else{
Code* tem = new Code;
p->zero = tem;
p = p->zero;
if (i+1 == (*j).end()){
p->flag = true;
}
}
}
else{
if (p->one != NULL){
p = p->one;
if (p->flag){
cout << "Set " << count << " is not immediately decodable" << endl;
outputflag = true;
break;
}
if (i+1 == (*j).end()){
p->flag = true;
}
}
else{
Code* tem = new Code;
p->one = tem;
p = p->one;
if (i+1 == (*j).end()){
p->flag = true;
}
}
}
}
}
if (!outputflag){
cout << "Set " << count << " is immediately decodable" << endl;
}
root.one = root.zero = NULL;
input.clear();
outputflag = false;
}
system("pause");
return 0;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator