采用vector保存string对象,按照从小到大的长度逐个检查前缀情况
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <cctype>
#include <algorithm>
#include <cmath>
using namespace std;
///
#define INF 0xffffff7
#define MAXN 200
///
vector<string> codes;
int cmp(const string &A, const string &B)
{
return A.size() < B.size();
}
int main()
{
///
int i, j, th;
string temp;
int cases = 1;
while (cin >> temp)
{
codes.clear();
codes.push_back(temp);
while (cin >> temp)
{
if (temp == "9")
break;
codes.push_back(temp);
}
sort(codes.begin(), codes.end(), cmp);
bool flag = true;
vector<string>::iterator it = codes.begin();
while (it != codes.end())
{
// cout << *it << endl;
vector<string>::iterator next = it + 1;
while (next != codes.end())
{
string::size_type position = (*next).find(*it);
if (position == 0)
{
flag = false;
break;
}
next++;
}
if (!flag)
break;
it++;
}
if (flag)
printf("Set %d is immediately decodable\n", cases);
else
printf("Set %d is not immediately decodable\n",cases);
cases++;
}
///
return 0;
}