zoukankan      html  css  js  c++  java
  • hdu 1251 统计难题

    字典树。题目并无难度,关键是怎样输入空格推出循环

    用getline输入C++的字符串要加上 #include<string>..........
    这题G++交会无限超内存
    #include<iostream>
    #include<cstdio>
    #include<string>
    using namespace std;
    string str;
    struct stu
    {
    	int m;
    	stu *a[26];
    	stu()
    	{
    		m=0;
    		for(int i=0;i<26;i++) a[i]=NULL;
    	}
    };
    stu *p=new stu();
    void insert(stu *root,int cnt)
    {
    	if(cnt==str.size()) return;
    	int x=str[cnt]-'a';
    	if(root->a[x]==NULL)
    	{
    		root->a[x]=new stu();
    	}
    	root=root->a[x];
    	root->m++;
    	insert(root,cnt+1);
    }
    void solve(stu *root,int cnt)
    {
    	int x=str[cnt]-'a';
    	root=root->a[x];
    	if(root==NULL)
    	{
    		cout<<"0"<<endl;
    		return;
    	}
    	if(cnt==str.size()-1)
    	{
    		cout<<root->m<<endl;
    	}
    	else solve(root,cnt+1);
    }
    int main()
    {
    	cin.sync_with_stdio(false);
    	while(getline(cin,str)&&str!="") insert(p,0);
    	while(cin>>str) solve(p,0);
    	return 0;
    } 



  • 相关阅读:
    网络请求与远程资源
    JavaScript对象
    微信小程序抓包Charles
    归并排序
    顺序表
    后缀表达式
    中缀表达
    ES6 Promise
    Es 方法
    10.26学习
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/8889190.html
Copyright © 2011-2022 走看看