hud2072单词数

为了练习trie树,我用trie树做。。。

#include
#define INF 0x3f3f3f3f
#define ll long long
#define mem(ar,num) memset(ar,num,sizeof(ar))
#define me(ar) memset(ar,0,sizeof(ar))
#define lowbit(x) (x&(-x))
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define lcm(a,b) ((a)*(b)/(__gcd((a),(b))))
#define Max 200001
#define mod 123456789
using namespace std;
int trie[Max][26], num[Max], tot, ans;
string s, str;
void ins(string s) {
    int root = 0;
    for(int i = 0; i < s.size(); i++) {
        int r = s[i] - 'a';
        if(!trie[root][r])
            trie[root][r] = ++tot;
        if(i == s.size() - 1) {
            if(!num[trie[root][r]])
                ans++;
            num[trie[root][r]] = 1;
        }
        root = trie[root][r];
    }
}
int main() {
    while(getline(cin, s) && s != "#") {
        me(num), me(trie), ans = 0;
        while(s.find(' ') != -1) {
            str = s.substr(0, s.find(' '));
            s.erase(0, s.find(' ') + 1);
            ins(str);
        }
        ins(s);
        cout << ans << endl;
    }
    return 0;
}

 

你可能感兴趣的:(字符串)