随笔分类 - 数据结构
摘要:简单的AC自动机; 1 #include 2 #include 3 #include 4 #define maxn 150005 5 using namespace std; 6 7 struct node 8 { 9 int cnt; 10 int id; 11 node *a[26],*tail; 12 }no[maxn]; 13 int nonocount; 14 node *newnode() 15 { 16 node *p=no+nonocount++; 17 p->cnt=0; 18 p->id=-1; 19 ...
阅读全文
摘要:简单KMP: 1 #include 2 #include 3 #define maxn 1000009 4 using namespace std; 5 6 char s[maxn]; 7 int next[maxn],n; 8 9 void getnext()10 {11 int j=-1,i=0;12 next[0]=-1;13 while(i0&&i%(i-next[i])==0)35 printf("%d %d\n",i,i/(i-next[i]));36 puts("");37 }38 ret...
阅读全文
摘要:字典树;不过数据量很大;4000*1000;我用的是传统的方法建树,虽然优化了一下,边插入边统计,不过还是T了;这是我的代码: 1 #include 2 #include 3 #define maxn 4000009 4 using namespace std; 5 int nonocount; 6 long long ans; 7 struct node 8 { 9 int num;10 node *a[62];11 } no[maxn];12 13 node* newnode()14 {15 node *p=no+nonocount++;16 p->num=...
阅读全文
摘要:一个简单的字典树上的dp;代码: 1 #include 2 #include 3 #include 4 #define maxn 400000 5 #define mod 20071027 6 using namespace std; 7 priority_queue,greater >q; 8 struct node 9 {10 bool f;11 node *a[26];12 } no[maxn];13 int nonocount;14 int d[maxn];15 bool vis[maxn];16 node *newnode()17 {18 node *p=no...
阅读全文
摘要:逆序对数的应用;逆序对数的写法有,二分,树状数组,分治;学习一下;树状数组版:代码: 1 #include 2 #include 3 #include 4 using namespace std; 5 const int maxn=100010; 6 int a[maxn],b[maxn],c[maxn]; 7 int n; 8 struct point 9 {10 int num,index;11 bool operator0)35 {36 ans+=c[x];37 x-=lowbit(x);38 }39 return an...
阅读全文
摘要:题目要求每次输出中间的那个数,如果数据很大肯定扛不住;所以用两个优先队列来维护;这样的话中间的那个数反正会在两个队列的任何一个的头部;时间复杂度肯定比较小;代码: 1 #include 2 #include 3 using namespace std; 4 int l1,l2; 5 priority_queue q1; 6 priority_queue, greater > q2; 7 void add(int x) 8 { 9 q2.push(x);10 l2++;11 if(!q1.empty() && !q2.empty())12 {13 ...
阅读全文
摘要:一个简单的线段树;被我看错题了,浪费一个半小时; 1 #include 2 #define maxn 500005 3 using namespace std; 4 5 struct tree 6 { 7 int l,r,value; 8 tree *right,*left; 9 }tr[maxn];10 int nonocount;11 int ans;12 13 void build(tree *rt,int l,int r)14 {15 rt->l=l;16 rt->r=r;17 if(l==r)18 {19 scanf(...
阅读全文
摘要:字典树,可惜比赛的时候有两句话写倒了;害得我调了一个小时;今天不宜做题 = =代码: 1 #include 2 #include 3 #define maxn 2600009 4 using namespace std; 5 6 struct node 7 { 8 bool flag; 9 int cnt;10 node *a[26];11 } no[maxn];12 13 char s[100];14 int ans,nonocount;15 node *newnode()16 {17 node *p=no+nonocount++;18 p->fla...
阅读全文
摘要:RMQ问题;采用游程编码;代码: 1 #include 2 #include 3 #include 4 #define maxn 100009 5 using namespace std; 6 7 int d[maxn][30],value[maxn],cnt[maxn],num[maxn],left[maxn],right[maxn]; 8 9 void RMQ_init(int n)10 {11 for(int i=1; ir)return 0;20 int k=0;21 while(1<<(k+1)<=r-l+1)k++;22 return max(d...
阅读全文
摘要:树状数组,把他们的技能值作为轴;首先按照编号从小到大插入值,这样就可以得到,技能值比当前小的人数;然后按照编号从大到小再插一遍;代码: 1 #include 2 #include 3 #define maxn 20005 4 using namespace std; 5 6 int a[maxn],c[100010]; 7 int l[maxn],r[maxn]; 8 int lowbit(int x){return x&-x;} 9 void add(int x,int d){while(x0){ret+=c[x],x-=lowbit(x);}14 return ret;15 }1.
阅读全文
摘要:用两个优先队列来实现,因为队列只能从一头出去;所以维护一个数组,来标记这个队列的已经出列而另外一个队列没有出列的元素;到时候再把他们删了就行; 1 #include 2 #include 3 #include 4 #define maxn 1000009 5 using namespace std; 6 7 priority_queue,greater >gq; 8 priority_queue,less >lq; 9 int numg[maxn],numl[maxn];10 int main()11 {12 int n,x,k;13 while(scanf("%d&qu
阅读全文
摘要:链表的应用;le[i]表示第i个元素左边的那个元素的标号;ri[i]表示第i个元素右边的那个元素的标号;代码: 1 #include 2 #include 3 #define maxn 100009 4 using namespace std; 5 6 char s[maxn]; 7 int le[maxn],ri[maxn]; 8 9 int main()10 {11 while(gets(s+1)!=NULL)12 {13 le[0]=ri[0]=0;14 for(int i=1; s[i]; i++)15 {16 ...
阅读全文
摘要:带权值的并查集的应用;代码: 1 #include 2 #include 3 #include 4 #include 5 #define maxn 20005 6 using namespace std; 7 8 int f[maxn]; 9 int d[maxn];10 int t,n,x,y;11 char s[5];12 13 int find(int x)14 {15 if(f[x]!=x)16 {17 int r=find(f[x]);18 d[x]+=d[f[x]];19 return f[x]=r;20 }...
阅读全文
摘要:并查集的简单应用:代码: 1 #include 2 #define maxn 100005 3 using namespace std; 4 int f[maxn]; 5 int find(int x){return x==f[x]?x:f[x]=find(f[x]);} 6 int main() 7 { 8 int x,y; 9 while(scanf("%d",&x)!=EOF)10 {11 for(int i=0;i<maxn;i++)f[i]=i;12 int cnt=0;13 while(x!=-1)14 ...
阅读全文
摘要:多路并归问题:代码: 1 #include 2 #include 3 #include 4 #define maxn 760 5 using namespace std; 6 7 struct node 8 { 9 int s,b;10 node(int s,int b):s(s),b(b) {}11 bool operatort.s;14 }15 };16 17 void merge(int *a,int *b,int *c,int n)18 {19 priority_queueq;20 for(int i=0; i<n; i++)21 ...
阅读全文
摘要:简单的优先队列的应用;代码: 1 #include 2 #include 3 using namespace std; 4 5 struct node 6 { 7 int num; 8 int ti; 9 int period;10 bool operatort.num;13 return ti>t.ti;14 }15 };16 priority_queueq;17 char s[20];18 int main()19 {20 int m;21 node a;22 while(scanf("%s",&s)&&s[...
阅读全文
摘要:map的用法:代码: 1 #include 2 #include 3 #include 4 #define maxn 1000009 5 using namespace std; 6 7 vectorve[maxn]; 8 map >mp; 9 int n,m,x,y;10 int main()11 {12 while(scanf("%d%d",&n,&m)!=EOF)13 {14 mp.clear();15 for(int i=0;i();19 mp[x].push_back(i+1);20 }21 ...
阅读全文
摘要:题目很简单,就是不知道为啥总是runtime error;后来发现我是先判断的y!=p.front();应该先判断是否为空;哎!今天果然不宜A题啊!代码: 1 #include 2 #include 3 #include 4 using namespace std; 5 priority_queuepq; 6 queueq; 7 stacks; 8 bool flag[4]; 9 int main()10 {11 int n,x,y;12 while(scanf("%d",&n)!=EOF)13 {14 while(!q.empty())q.pop();...
阅读全文
摘要:一个离散化的简单题;我用的是STL来做的离散化;好久没写离散化了,纪念一下!代码: 1 #include 2 #include 3 #include 4 #include 5 #define maxn 5005 6 using namespace std; 7 vectorve; 8 int mmb[12]={0,44640,84960,129600,172800, 9 217440,260640,305280,349920,393120,437760,480960};10 struct node11 {12 int st,end;13 }no[maxn];14 15 char s[1...
阅读全文
摘要:回文串的题,求最大的双重回文串;重新复习了一下manacher算法;代码: 1 #include 2 #include 3 #include 4 #define M 310010 5 using namespace std; 6 char b[M],a[M='A')17 b[i]=b[i]-'A'+'a';18 memset(a,0,sizeof a);19 n=0;20 a[n++]='?';21 a[n++]='#';22 for(i=1; b[i]!='\0'; i++)23 ...
阅读全文