What Are You Talking About HDU – 1075

Miku

字典树部分非常好解决,只要把最后每个单词结尾处记录下翻译成了啥

但是输出很麻烦,要处理空格,回车,换行和标点符号

不过有string在,还行

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<istream>
using namespace std;
int n;
string s;
struct t{
    int ne[28];
    int cnt;
}tr[400001];
int p;
int l;
int x;
int po;
void add(){
    //cout<<s<<" "<<l<<endl;
    p=0;
    for(int i=0;i<l;++i){
        x=s[i]-'a'+1;//这么干就不会有0了 
        if(!tr[p].ne[x]){//没有就建 
            po++;
            tr[p].ne[x]=po;     
        }
        p=tr[p].ne[x];
        tr[p].cnt++;//被经过次数即为前缀数 
    }
    return ;
}
int query(){
    p=0;
    for(int i=0;i<l;++i){
        x=s[i]-'a'+1;
        if(!tr[p].ne[x]){ 
            return 0;
        }else{
            p=tr[p].ne[x];
        }
    }
    return tr[p].cnt;
}

int main(){
    while(getline(cin,s)){
        l=s.length();
        if(l==0)
        break;
        add();
    }   
    while(getline(cin,s)){
        l=s.length();
        cout<<query()<<endl;;
    }
    return 0;
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇