Uva 644 - Immediate Decodability
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=8&page=show_problem&problem=585
C++ code :
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
vector<string> setNum;
int main() {
char c; int count=1;
while (scanf("%c", &c)!=EOF) {
setNum.clear();
while (c!='9') {
string vTemp;
while (c!='\n') {
vTemp.insert(vTemp.end(), c);
scanf("%c", &c);
}
setNum.push_back(vTemp);
scanf("%c", &c);
}
sort(setNum.begin(), setNum.end());
bool prefix=false;
for (int i=0; i<setNum.size(); i++) {
for (int j=i+1; j<setNum.size(); j++) {
size_t str= setNum[j].find(setNum[i]);
if (str==0) {
prefix=true;
break;
}
}
if (prefix)
break;
}
if (prefix)
printf("Set %d is not immediately decodable\n", count);
else
printf("Set %d is immediately decodable\n", count);
count++;
if (scanf("%c", &c)==EOF)
break;
}
return 0;
}
C++ code :
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
vector<string> setNum;
int main() {
char c; int count=1;
while (scanf("%c", &c)!=EOF) {
setNum.clear();
while (c!='9') {
string vTemp;
while (c!='\n') {
vTemp.insert(vTemp.end(), c);
scanf("%c", &c);
}
setNum.push_back(vTemp);
scanf("%c", &c);
}
sort(setNum.begin(), setNum.end());
bool prefix=false;
for (int i=0; i<setNum.size(); i++) {
for (int j=i+1; j<setNum.size(); j++) {
size_t str= setNum[j].find(setNum[i]);
if (str==0) {
prefix=true;
break;
}
}
if (prefix)
break;
}
if (prefix)
printf("Set %d is not immediately decodable\n", count);
else
printf("Set %d is immediately decodable\n", count);
count++;
if (scanf("%c", &c)==EOF)
break;
}
return 0;
}
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home