LA 3942 背单词(dp+Trie)
2016-05-08 09:51
260 查看
分析:d[i]=sum{ d(i+len(x) } [i,L]的种类数
可以正向枚举,也可以逆向枚举,Trie+dp的结合题,第一次见,其实dp的思维还是比较简单的
<pre class="cpp" name="code">#include <cstdio>#include <cstring>#include <string>#include <iostream>#include <algorithm>using namespace std;const int mod=20071027;struct TrieNode{bool vis;struct TrieNode *next[26];};typedef TrieNode Trie;Trie *root;char str[300006];long long dp[300006];Trie *CreateNode(){Trie *p=(Trie*)malloc(sizeof(Trie));p->vis=false;for (int i=0;i<26;i++)p->next[i]=NULL;return p;}void Insert(string str){Trie *p=root;int len=str.size();for (int i=0;i<len;i++){int id=str[i]-'a';if (p->next[id] == NULL){p->next[id]=CreateNode();p=p->next[id];}else p=p->next[id];}p->vis=true;}void Search(int ii,int len){Trie *p=root;dp[ii]=0;for (int i=ii;i<len;i++){int id=str[i]-'a';p=p->next[id];if (p == NULL) break;if (p->vis == true) dp[ii]=(dp[ii] + dp[i+1]) % mod;}}int main(){int j=0;while (~scanf("%s",str)){root=CreateNode();int len=strlen(str);int n;scanf("%d",&n);string s;for (int i=1;i<=n;i++){cin>>s;Insert(s);}dp[len]=1;//精髓for (int i=len-1;i>=0;i--){Search(i,len);}printf("Case %d: %d\n",++j,dp[0]);}}
相关文章推荐
- SurfaceView的用法
- selenium_采集药品数据1_采集第一页表格
- (1)NSThread
- swift基础学习(04)[闭包、函数、枚举、类和结构体]
- zookeeper分布式锁
- 为sql server 增加 parseJSON 和 ToJSON 函数
- zookeeper分布式锁
- C#版 选择法、冒泡法、插入法和快速排序法分析与对比
- java 死锁产生原因及解锁
- azure 云上 oracle11.2.0.4里dataguard归档日志传输 1034 问题详细解决过程
- java或者jsp中修复会话标识未更新漏洞
- 工厂模式
- 10期4月份博客期刊
- [shell] 文本操作
- [转]javascript中基本类型和引用类型的区别分析
- linux grep
- linux grep
- Vim for Python & Normal Plugins & Vundle
- ios Sqlite 的基本使用
- Java千百问_06数据结构(012)_如何遍历数组