7.25T1T2 • water problem

Two water problem, the thieves are miserable, uncomfortable. . . . . . . A lot of sad memories of their own as well as konjac, this morning, I definitely like zz

T1

emm, KMP, hash your choice, of course, I forgot how to play KMP, will play a hash, I think this formula would also like for a long time, but the conversion between points and the length of the last or was I playing dead, no and shift right, but I finally hash off, hash, then sweep on O (n) length again on the line, can be used as hash template title, or I waste ah, the other on nothing more to say, pay attention to small details on it

 1 #include<cstdio>
 2 #include<string>
 3 #include<cstring>
 4 #include<iostream>
 5 #define ull unsigned long long
 6 #define p 131
 7 #define maxn 200100
 8 using namespace std;
 9 int t,lena,lenb,maxxx;
10 char a[maxn*2],b[maxn*2];
11 ull mi[maxn*2],hasha[maxn*2],hashb[maxn*2];
12 void clear()
13 {
14     maxxx=0;  memset(hasha,0,sizeof(hasha));  memset(hashb,0,sizeof(hashb));
15 }
16 int main()
17 {
18     scanf("%d",&t);  mi[0]=1;
19     for(int i=1;i<=maxn*2;++i)  mi[i]=mi[i-1]*p;
20     while(t--)
21     {
22         clear();
23         scanf("%d%d%s",&lena,&lenb,a+1);
24         for(int i=1;i<=lenb;++i)  b[i]=a[i];
25         scanf("%s",&b[++lenb]);
26         for(int i=1;i<=lena;++i)  hasha[i]=hasha[i-1]*p+a[i]-'a'+1;
27         for(int i=1;i<=lenb;++i)  hashb[i]=hashb[i-1]*p+b[i]-'a'+1;
28         if(lenb<=lena&&hashb[lenb]==hasha[lenb])  maxxx=lenb;
29         for(int i=1;i<=min(lena,lenb);++i)
30         {
31             ull bijiao=hashb[lenb]-hashb[lenb-i]*mi[i];
32             if(bijiao==hasha[i])  maxxx=max(maxxx,i);
33         }
34         printf("%d\n",maxxx);
35     }
36     return 0;
37 }
Violence miracle

 

T2

Read a good question, multi-draw sample, to ponder, do not tell me, and thinking about what is what, kick throw, I waste too much waste, and this question, at first glance gives you a Da Banzai cut point is the illusion of seeking positive solutions, in fact or in the pit, is from 1 to n is certainly a necessary point cut point, but not necessarily cut point is a necessary point, because there may be from 1 to n certain point cut do not need to go through, so this title you give him double reduction point, and then went dfs from 1 n, marking what point the way to cut through, and those cut point is the final answer, remember to open slightly larger array, please remember correspondence relationship before and after condensing point no problem

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<vector>
  4 #include<stack>
  5 #include<cstring>
  6 #define maxn 200100
  7 #define maxm 400100
  8 using namespace std;
  9 int T,n,m,js,root,tot,cnt,num,j,ans;
 10 int head[maxn],to[maxm*2],xia[maxm*2];
 11 int dfn[maxn],low[maxn],cut[maxn];
 12 int bh[maxn],ss[maxn];
 13 int h[maxn*2],t[maxm*2],x[maxm*2];
 14 int pd[maxn*2],ys[maxn*2];
 15 vector <int> ds[maxn*2];
 16 stack <int> s;
 17 void clear()
 18 {
 19     js=0;  root=0;  tot=0;  num=0;  j=0;  ans=0;
 20     while(s.empty()==false)  s.pop();
 21     for(int i=1;i<=cnt;++i)  ds[i].clear();
 22     memset(head,0,sizeof(head));  memset(to,0,sizeof(to));
 23     memset(xia,0,sizeof(xia));  memset(dfn,0,sizeof(dfn));
 24     memset(low,0,sizeof(low));  memset(cut,0,sizeof(cut));
 25     memset(bh,0,sizeof(bh));  memset(ss,0,sizeof(ss));
 26     memset(h,0,sizeof(h));  memset(t,0,sizeof(t));  memset(x,0,sizeof(x));
 27     memset(pd,0,sizeof(pd));  memset(ys,0,sizeof(ys));
 28     cnt=0;
 29 }
 30 void add(int x,int y)
 31 {
 32     to[++js]=y;  xia[js]=head[x];  head[x]=js;
 33 }
 34 void tarjan(int x)
 35 {
 36     int bj=0;  dfn[x]=low[x]=++tot;  s.push(x);
 37     if(x==root&&head[x]==0)  {ds[++cnt].push_back(x);  return ;}
 38     for(int i=head[x];i;i=xia[i])
 39     {
 40         int ls=to[i];
 41         if(dfn[ls]==0)
 42         {
 43             tarjan(ls);  low[x]=min(low[x],low[ls]);
 44             if(low[ls]>=dfn[x])
 45             {
 46                 bj++;
 47                 if(x!=root||bj>=2)  cut[x]=1;
 48                 int y;  cnt++;
 49                 do  {y=s.top();  s.pop();  ds[cnt].push_back(y);}
 50                 while(y!=ls);
 51                 ds[cnt].push_back(x);
 52             }
 53         }
 54         else  low[x]=min(low[x],dfn[ls]);
 55     }
 56 }
 57 void ADD(int a,int b)
 58 {
 59     t[++j]=b;  x[j]=h[a];  h[a]=j;
 60 }
 61 void dfs(int w)
 62 {
 63     ys[w]=1;
 64     if(pd[w]==1)  return ;
 65     for(int i=h[w];i;i=x[i])
 66     {
 67         int ls=t[i];
 68         if(ys[ls]==0)
 69         {    
 70             dfs(ls);
 71             if(pd[ls]==1)  pd[w]=1;
 72         }
 73     }
 74 }
 75 int main()
 76 {
 77     scanf("%d",&T);
 78     while(T--)
 79     {
 80         scanf("%d%d",&n,&m);
 81         for(int i=1;i<=m;++i)
 82         {
 83             int u,v;  scanf("%d%d",&u,&v);
 84             add(u,v);  add(v,u);
 85         }
 86         for(int i=1;i<=n;++i)
 87             if(dfn[i]==0)  {root=i;  tarjan(i);}
 88         num=cnt;
 89         for(int i=1;i<=n;++i)
 90             if(cut[i]==1)  bh[i]=++num;
 91         for(int i=1;i<=cnt;++i)
 92             for(int j=0;j<ds[i].size();++j)
 93             {
 94                 int ls=ds[i][j];
 95                 if(cut[ls]==1)  {ADD(i,bh[ls]);  ADD(bh[ls],i);}
 96                 else  ss[ls]=i;
 97             }
 98         if(cut[n]==1)  pd[bh[n]]=1;
 99         else  pd[ss[n]]=1;
100         if(cut[1]==1)  dfs(bh[1]);
101         else  dfs(ss[1]);
102         for(int i=2;i<n;++i)
103         {
104             int ls;
105             if(cut[i]==1)  ls=bh[i];
106             else  ls=ss[i];  
107             if(pd[ls]==1&&cut[i]==1)  ans++;
108         }
109         printf("%d\n",ans);
110         for(int i=2;i<n;++i)
111         {
112             int ls;
113             if(cut[i]==1)  ls=bh[i];
114             else  ls=ss[i];  
115             if(pd[ls]==1&&cut[i]==1)  printf("%d ",i);
116         }
117         puts("");  clear();
118     }
119     return 0;
120 }
Template miracle

 

Guess you like

Origin www.cnblogs.com/hzjuruo/p/11246465.html