I don't know which method is used by SO, but:
I suppose a fast (and very simplistic) way of doing this is by going back to C, and checking them one by one, maybe with a KMP algorithm.
Another (not so simple) way of doing this, is by keeping a trie with those 10.000 words and searching the text using that. This would be super-fast, but fairly hard to implement. If you are interested, I have a dummy implementation in C++.
EDIT
Looking back to it, I see I've only used fstream, so this could be modified easily for C, so you'll be able to integrate with python easily . This is the source:
#include <fstream>
using namespace std;
ifstream in("trie.in");
ofstream out("trie.out");
struct Trie
{
short nr, pref;
Trie *children[26], *father;
Trie()
{
int i;
nr = pref = 0;
for(i=0; i<26; i++)
children[i] = NULL;
father = NULL;
}
};
Trie t, *it, *it2;
int n, op, val, i, l, len;
char s[22],*p;
int main()
{
while(in>>op>>s)
{
p = s;
it = &t;
l = 0;len=0;
while(p[0] != '\0')
{
if(it->children[p[0] - 'a'] == NULL && op == 2)
{op=9; out<<"0\n"; break;}
if(it->children[p[0] - 'a'] == NULL && op == 3)
break;
if(it->children[p[0] - 'a'] == NULL)
it->children[p[0] - 'a'] = new Trie(), it->children[p[0] - 'a']->father = it,
it = it->children[p[0] - 'a'];
else
it = it->children[p[0] - 'a'];
if(op == 0)
++ it->pref;
else if(op == 1 && it->pref > 0)
-- it->pref;
else if(op == 3 && it->pref > 0)
l = p-s+1;
p++;
}
if(op == 0)
it->nr ++;
else if(op == 1 && it->nr > 0)
{
it->nr --;
l = strlen(s)-1;
while(it->pref == 0 && it != &t && l>=0)
{
it2 = it->father;
it2->children[s[l--] - 'a'] = NULL;
delete it;
it = it2;
}
}
else if(op == 2)
out<<it->nr<<'\n';
else if(op == 3)
out<<l<<'\n';
}
return 0;
}
This takes in trie.in
text formatted like this:
0 lat
0 mare
0 lac
2 la
0 mare
1 lat
0 ma
0 lung
3 latitudine
0 mari
2 mare
0 lat
0 mic
3 latime
2 lac
3 mire
And produces text like this
0
2
2
3
1
2
0 w - add the word w in the list (could be multiple times)
1 w - delete one record of the word w from the list (could be multiple times)
2 w - print how many w words are there in the list
3 w - print the length of the longest common prefix of w with any other word in the list
Oh, and sorry for the poor formatting, this was done for training.