448B

Problem Link

 

View Code
#include<bits/stdc++.h>
using namespace std;
#define all(s) sort(s.begin(),s.end())
string s,t;
bool ar()
{all(s);all(t);
return s==t;
}

bool autom()
{
int c=0,sz=0;
int l=s.size();
for(int i=0;i<l;i++)
{
if(s[i]==t[c])
c++,sz++;
}

return sz==t.size()?1:0;

}
bool bt()
{
all(s);
all(t);
int c=0,sz=0;
int l=s.size();
for(int i=0;i<l;i++) { if(s[i]==t[c]) c++,sz++; } return sz==t.size()?1:0; } int main() { cin>>s>>t;
if(autom())
cout<<"automaton"<<endl;
else if(ar())
cout<<"array"<<endl;
else if(bt())
cout<<"both"<<endl;
else
cout<<"need tree"<<endl;

}

 

Advertisements

GSS1

#Problem Link

Prerequisites:

1.  e-maxx

 

 

View Code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mx = 50010;
ll a[mx];

struct data {
int sum, pref, suff, ans ;
}tree[4*mx] ;
data make_data ( ll val ) {
data res ;
res. sum = val ;
res. pref = res. suff = res. ans = val ;
return res ;
}
data combine ( data l, data r ) {
data res ;
res. sum = l. sum + r. sum ;
res. pref = max ( l. pref , l. sum + r. pref ) ;
res. suff = max ( r. suff , r. sum + l. suff ) ;
res. ans = max ( max ( l. ans , r. ans ) , l. suff + r. pref ) ;
return res ;
}

void build(ll node,ll b,ll e)
{
if(b==e)
{
tree[node]=make_data(a[b]);
}
else{
int mid=(b+e)/2;
build(node*2,b,mid);
build(node*2+1,mid+1,e);
tree[node]=combine(tree[node*2],tree[node*2+1]);
}
}
void buildsm(ll node,ll b,ll e)
{

}

ll update(ll node,ll b,ll e,ll x,ll y)
{

}

data query(ll node,ll b,ll e,ll l,ll r)
{
if(l==b &&e==r)
return tree[node];
int mid=(b+e)/2;
if(r<=mid) return query(node*2,b,mid,l,r); if(l>mid)
return query(node*2+1,mid+1,e,l,r);
return combine(query(node*2,b,mid,l,mid),query(node*2+1,mid+1,e,mid+1,r));
};
ll n,q;
int main()
{

scanf(“%lld”,&n);
memset(tree,0,sizeof tree);
memset(a,0,sizeof a);
for(ll i=1;i<=n;i++)
{scanf(“%lld”,&a[i]);
}
build(1,1,n);
//buildsum(1,1,n);
scanf(“%lld”,&q);
while(q–)
{
ll x,y;
scanf(“%lld %lld”,&x,&y);
{
printf(“%lld\n”,query(1,1,n,x,y).ans);
//cout<<query(1,1,n,x,y)<<endl;
}

}

}

 

 

126B – Password

This problem is based on Z- algorithm

To learn this algorithm  – Click here!

 

View Code

#include<bits/stdc++.h>
using namespace std;
void solve(string s)
{
int l=s.size();
int z[l];
int left,right;
left=right=0;
for(int k=1;k<l;k++) { if(k>right)
{
left=right=k;
while(right {
right++;
}
z[k]=right-left;
right–;
}
else{
int k1=k-left;
if(z[k1]<right-k+1)
{
z[k]=z[k1];
}
else{
left=k;
while(right {
right++;
}
z[k]=right-left;
right–; }

}
}
int mxz = 0, res = 0,n=l;
for(int a=1;a<l;a++){
//printf(“%d %d\n”, a, Z[a]);
if(a + z[a] == l){
if(z[a] <= mxz){
for(int j=0;j<z[a];j++) printf(“%c”, s[j]); return ; } } mxz = max(mxz, z[a]); } puts(“Just a legend”); } int main() { string s; cin>>s;

solve(s);
}

 

Z algorithm

Today we will try with a new algorithm – Z algorithm

This algo is used in pattern matching,there is another algo for pattern matching

This algorithm works in O(length of text+length of pattern) complexity

and space complexity is similar to time complexity that means O(length of text+length of pattern)

This algorithm is very easy.

</>Video Tutorial

 

</>Visualization

 

</> Blog

 

#Problemset