先将一行读入,然后再分割。
- 判断节点:看尾结点是否被标记。
- stringstream用法
- 创建流对象
使用strignstream显式创建对象- 给流对象赋值
ss<<Object
可以为int, string, double- 流对象的清除
ss.clear() 或 ss.str("")
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<sstream>
using namespace std;
const int maxn =2e6+5;
int tree[maxn][30];
int tot,res;
string str1,str2;
bool mark[maxn];
void insert()
{
int len=str2.size();
int root=0;
for(int i=0;i<len;i++)
{
int id=str2[i]-'a';
if(!tree[root][id]){
tree[root][id]=++tot;
}
root=tree[root][id];
}
if(mark[root]==false) res++, mark[root]=true;
}
int main()
{
ios::sync_with_stdio(false);
while(getline(cin,str1))
{
if(str1=="#") break;
int ans=0;
stringstream ss(str1);
while(ss>>str2)
insert();
cout<<res<<endl;
for(int i=0;i<=tot;i++)
{
mark[i]=false;
for(int j=0;j<30;j++)
tree[i][j]=0;
}
tot=res=0;
}
return 0;
}