标签:php acm ons ++ tree max namespace ems printf
字典树模板题
题目代码
#include<stdio.h> #include<iostream> #include<string.h> #include<sstream> using namespace std; const int maxn=1e4+7; string str1,str2; int tree[maxn][30],flag[maxn],tot; bool insert(string s){ int len=s.size(); int rt=0; for(int i=0;i<len;i++){ int id=s[i]-‘a‘; if(!tree[rt][id])tree[rt][id]=++tot; rt=tree[rt][id]; } if(flag[rt])return false; flag[rt]=1; return true; } void init(){ tot=0; memset(tree,0,sizeof(tree)); memset(flag,0,sizeof(flag)); } int main(){ while(getline(cin,str1)){ if(str1=="#")return 0; init(); int sum=0; stringstream is(str1); while(is>>str2){ if(insert(str2))sum++; } printf("%d\n",sum); } return 0; }
标签:php acm ons ++ tree max namespace ems printf
原文地址:https://www.cnblogs.com/helman/p/11304731.html