1.机器学习概念:
残差:实际值-预测值=残差
https://bigquant.com/community/t/topic/125885
#include "cstdio" #include "cstring" #include "iostream" #include "queue" #include "string" using namespace std; struct Trie { Trie *next[26],*fail; int cnt; }*root; struct status { Trie *last; int cnt; status(Trie *last,int cnt):last(last),cnt(cnt) {} }; Trie *newnode() { Trie *ret=new Trie; memset(ret->next,0,sizeof(ret->next)); ret->fail=0; ret->cnt=0; return ret; } void init() {root=newnode();} void Insert(string str) { Trie *pos=root; for(int i=0;i<str.size();i++) { int c=str[i]-'a'; if(!pos->next[c]) pos->next[c]=newnode(); pos=pos->next[c]; } pos->cnt++; } void getfail() { queue<Trie *> Q; for(int c=0;c<26;c++) { if(root->next[c]) { root->next[c]->fail=root; Q.push(root->next[c]); } else root->next[c]=root; } while(!Q.empty()) { Trie *x=Q.front();Q.pop(); for(int c=0;c<26;c++) { if(x->next[c]) { x->next[c]->fail=x->fail->next[c]; Q.push(x->next[c]); } //although not x->next[c],also make connect else x->next[c]=x->fail->next[c]; } } } int find(string str) { Trie *pos=root,*last; queue<status> Q; int ans=0; for(int i=0;i<str.size();i++) { int c=str[i]-'a';last; if(pos->next[c]) { pos=pos->next[c]; last=pos; while(last->cnt) { Q.push(status(last,last->cnt)); ans+=last->cnt; last->cnt=0; //ÐÞ¸Älast->cnt last=last->fail; } } } while(!Q.empty()) //»Ö¸´last->cnt { status x=Q.front();Q.pop(); x.last->cnt=x.cnt; } return ans; } int main() { freopen("b.txt","r",stdin); ios::sync_with_stdio(false); int T,n; string tt; cin>>T; while(T--) { cin>>n; init(); for(int i=1;i<=n;i++) { cin>>tt; Insert(tt); } getfail(); cin>>tt; int ans=find(tt); printf("%d\n",ans); } }
#include<bits/stdc++.h> using namespace std; struct Trie{ Trie *next[26],*fail; int cnt; }*root; struct status{ Trie *last; int cnt; status(Trie *last,int cnt):last(last),cnt(cnt){} }; Trie* newNode() { Trie *pos=new Trie; memset(pos->next,0,sizeof(pos->next)); pos->fail=0; pos->cnt=0; return pos; } void init(){root=newNode();} void Insert(string str) { Trie *pos=root; for(int i=0;i<str.size();++i) { int c=str[i]-'a'; if(!pos->next[c]) { pos->next[c]=newNode(); } pos=pos->next[c]; } pos->cnt++; } void getfail() { queue<Trie*> Q; for(int i=0;i<26;++i) { if(root->next[i]) { root->next[i]->fail=root; Q.push(root->next[i]); } else root->next[i]=root; } while(!Q.empty()) { Trie *q=Q.front();Q.pop(); for(int i=0;i<26;++i) { if(q->next[i]) { q->next[i]->fail=q->fail->next[i]; //q already in.so just push next part Q.push(q->next[i]); } else { //also not also connect q->next[i]=q->fail->next[i]; } } } } int find(string str) { Trie *pos=root,*last; queue<status> Q; int ans=0; for(int i=0;i<str.size();++i) { int c=str[i]-'a'; if(pos->next[c]) { pos=pos->next[c]; last=pos; while(last->cnt) { ans+=last->cnt; Q.push(status(last,last->cnt)); last->cnt=0; last=last->fail; } } } while(!Q.empty()) { status q=Q.front();Q.pop(); q.last->cnt=q.cnt; } return ans; } int main() { ios::sync_with_stdio(false); freopen("a.in","r",stdin); int T,n; cin>>T; string tt; while(T--) { cin>>n; //each init update root == change tree,so each round update once init(); for(int i=0;i<n;++i) { cin>>tt; Insert(tt); } getfail(); cin>>tt; int ans=find(tt); cout<<ans<<endl; } return 0; }
2. 内存分区
(1)栈区(内存由系统控制)
int x = 0; int *p = NULL;
(2)堆区(new分配的内存将分配到堆区,需要程序员自己释放)
int *p = new int[20];
(3)全局区
存储全局变量和静态变量
(4)常量区
string str = “hello”;
(5)代码区
存储逻辑代码的二进制
————————————————
版权声明:本文为CSDN博主「sunniy27」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/sunniy27/article/details/55003467
3.LC 208
struct Trie1 { int val; bool end; Trie1* next[26]; }*root; Trie1* newNode() { Trie1* pos=new Trie1; //initial array val end return Trie memset(pos->next,0,sizeof(pos->next)); pos->val=0; pos->end=false; return pos; } class Trie { public: /** Initialize your data structure here. */ Trie() { root=newNode(); } /** Inserts a word into the trie. */ void insert(string word) { Trie1* pos=root; for(int i=0;i<word.size();++i) { //attention char to int change int c=word[i]-'a'; if(pos->next[c]==NULL) { pos->next[c]=newNode(); pos->next[c]->val=c; } pos=pos->next[c]; } pos->end=true; } /** Returns if the word is in the trie. */ bool search(string word) { Trie1* pos=root; for(int i=0;i<word.size();++i) { int c=word[i]-'a'; if(pos->next[c]==NULL) { return false; } pos=pos->next[c]; } if(pos->end!=true) return false; else return true; } /** Returns if there is any word in the trie that starts with the given prefix. */ bool startsWith(string prefix) { Trie1* pos=root; for(int i=0;i<prefix.size();++i) { int c=prefix[i]-'a'; if(pos->next[c]==0) { return false; } pos=pos->next[c]; } return true; } }; /** * Your Trie object will be instantiated and called as such: * Trie* obj = new Trie(); * obj->insert(word); * bool param_2 = obj->search(word); * bool param_3 = obj->startsWith(prefix); */
4.以往只用输出,所以感觉没区别。加了&代表引用,类似函数参数的用法,可以更改值
那么如果要遍历容器内的元素的同时又要修改元素的值该怎么做呢,方法就是将遍历的变量声明为引用类型,看下面例子:
std::vector<int> vec {1,2,3,4,5,6,7,8,9,10};
cout << "修改前" << endl;
for (auto& n :vec)
std::cout << n++;
cout << endl;
cout << "修改后" << endl;
for (auto j : vec)
std::cout << j;
代码的输出结果为:
修改前
12345678910
修改后
234567891011
可以看到,容器内的元素每个都加了1,两者的区别仅为在修改的时候,将声明的遍历遍历n从auto 声明为auto &,使用基于范围的For循环一定要注意这一点,基于范围的FOR循环的遍历是只读的遍历,除非将变量变量的类型声明为引用类型。
————————————————
版权声明:本文为CSDN博主「Rayen0715」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/hailong0715/article/details/54172848
浙公网安备 33010602011771号