Tuesday, October 11, 2016

UVa 10391 Compound Words

UVa 10391 Compound Words


#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
vector<string> words;

class kmap
{
public:
kmap (const int &v):defVal (v)
{
}
int &getVal(const string &k)
{
if (m.find(k) == m.end())
return defVal;
else
return m[k];
}
int &operator[] (const string &k)
{
return m[k];
}
std::map<string,int> m;
int defVal;
};

kmap ver(-1);

int main()
{
string word, s1, s2;
int i, j;

i=0;
while (cin >> word)
{
words.push_back(word);
ver[word]=i++;
}
int list_len = words.size();

for (i=0 ; i<list_len ; i++)
{
int wlen = words[i].length();
//cout << words[i] << endl;
for (j=1 ; j<wlen ; j++)
{
s1 = words[i].substr(0,j);
s2 = words[i].substr(j,wlen);

//cout << " " << ver[s1] << "--" << ver[s2] << endl;

if (ver.getVal(s1)!=-1 && ver.getVal(s2)!=-1 && ver[s1]!=i && ver[s2]!=i)
{
cout << words[i] << endl;
break;
}
}
}
return 0;
}


Go to link download