hdu 2072 (trie template, set, map can be done)

Address: http://acm.hdu.edu.cn/showproblem.php?pid=2072

lily has been a very good friend xiaoou333 empty, he thought something did not make sense of things, is an article of statistics the total number of different words. Below you xiaoou333 mission is to help solve this problem.

Input multiple sets of data, each line, each is a small article. Each small articles are lowercase letters and spaces, no punctuation, # indicates the end of input is encountered. Output per group output only an integer, which make the trip alone, the total number of different words in an article of the integer representative. Sample Input

you are my friend
#
Sample Output
4
    set做法:

    

#include<iostream>
#include<cstring>
#include<set>
#include<algorithm>
using namespace std;
typedef long long ll;
set<string>se;
int main()
{
        string s;
        while(getline(cin,s))
        {
            if(s[0]=='#')
            break;
            string m;
            se.clear();
            int len=s.length();
        //    cout<<"len: "<<len<<endl;
            for(int i=0;i<len;i++)
        {
            //    cout<<i<<" "<<s[i]<<endl;
                if(s[i]>='a'&&s[i]<='z')
                {
                    int j;
                    m.clear();
                    for(j=i;j<len&&s[j]>='a'&&s[j]<='z';j++)
                    {
                        m+=s[j];
                    }
                    i=j;
                    se.insert(m);
                }
        }
            cout<<se.size()<<endl;
}
return 0;
}

    

  istringstream practices (specifically I do not understand, anyway, you can remove the spaces):
    
#include<iostream>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<set>
using namespace std;
typedef long long ll;
set<string>q;
int main()
{    
    string s;
    while(getline(cin,s)){
        if(s[0]=='#')    
            break;
        istringstream sss(s);
        q.clear();
        string ss;
        while(sss>>ss){
            q.insert(ss);
        }
        cout<<q.size()<<endl;
    }
    return 0;
}

    Focus is on the dictionary tree operations, due to the understanding of the num array is not thorough, writing a bunch of BUG, ​​own it will not work ah.  

    Here num array record is: mark the end of the current string. For example qwe qw: First qwe obtained: 1 (q) 2 (w) 3 (e) 4

                                             0    0    1

                                  E represents ending joined

                                   Then qw, k = at 2, num = 1, w represents ending joined. That's two words.

      Code on it, began to see the automaton ac

       

#include<iostream>
#include<cstring>
#include<set>
#include<algorithm>
using namespace std;
typedef long long ll;
#include<map>
const int maxn=1e4+10;
int tr[maxn][29];
int num[maxn];
string s;
string mid;
int k=1;
int ans=0;
int join()
{
    int p=0;
     Int len = mid.length ();
     int OK1 = 0 , OK2 = 0 ;
     for ( int I = 0 ; I <len; I ++ ) 
    { 
        int C = MID [I] - ' A ' ;
         IF (! TR [ P] [C]) 
            { 
                TR [P] [C] = K ++ ; 
            } 
        P = TR [P] [C]; 
    } 
    IF (! NUM [P]) // end of the current word has not been marked, indicating that a new word. 
    { 
        NUM [P] = . 1 ;
        return 1;
    }
    else
        return 0;
}
int main()    //qq as a
{
    while(getline(cin,s))
    {
        if(s[0]=='#')    
            break;
        k=1;
        ans=0;
        memset(tr,0,sizeof(tr));
        memset(num,0,sizeof(num));
        int len=s.length();
        for(int i=0;i<len;i++)
        {
            if(s[i]>='a'&&s[i]<='z')
            {
                mid.clear();
                int j;
                for(j=i;j<len&&s[j]>='a'&&s[j]<='z';j++)
                {
                    mid+=s[j];
                }
                i=j;
            //    
                if(join()==1)
                {
                    ans++;
            //        cout<<mid<<" "<<ans<<endl;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

                                     

    

Guess you like

Origin www.cnblogs.com/liyexin/p/11599009.html