# Codeforces Round #244 (Div. 2)（强连通分量，后缀数组）

A. Police Recruits

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
int main()
{
//freopen("in.txt","r",stdin);
cin>>n;
int ans=0,x,p=0;
for(int i=0;i<n;i++)
{
cin>>x;
if(x<0)
{
if(p>0)p--;
else ans++;
}
else p+=x;
}
cout<<ans<<endl;
return 0;
}

B. Prison Transfer
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=200010;
int a[maxn];
int n,t,c;
int main()
{
cin>>n>>t>>c;
for(int i=1;i<=n;i++)cin>>a[i];
int ans=0,cnt=0;
for(int i=1;i<=c;i++)
if(a[i]>t)cnt++;
if(n==c&&cnt<=0)ans++;
for(int i=c+1;i<=n;i++)
{
if(cnt<=0)ans++;
if(a[i]>t)cnt++;
if(a[i-c]>t)cnt--;
if(i==n&&cnt<=0)ans++;
}
cout<<ans<<endl;
return 0;
}

C. Checkposts

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<stack>
#include<algorithm>
using namespace std;
const int maxn=100010;
const int INF=1000000000;
const int MOD=1000000007;
int n,m;
vector<int> grid[maxn],belong[maxn];
stack<int> s;
void dfs(int u)
{
s.push(u);
for(int i=0;i<grid[u].size();i++)
{
int v=grid[u][i];
if(!pre[v])
{
dfs(v);
}
}
{
++scc_cnt;
int x;
while(true)
{
x=s.top();s.pop();
sccno[x]=scc_cnt;
belong[scc_cnt].push_back(x);
if(x==u)break;
}
}
}
void find_scc()
{
dfs_clock=scc_cnt=0;
memset(pre,0,sizeof(pre));
memset(sccno,0,sizeof(sccno));
for(int i=1;i<=n;i++)
if(!pre[i])dfs(i);
}
int main()
{
//freopen("in.txt.txt","r",stdin);
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
cin>>m;
while(m--)
{
int u,v;
cin>>u>>v;
grid[u].push_back(v);
}
find_scc();
long long ans=0,p=1;
//cout<<scc_cnt<<endl;
for(int i=1;i<=scc_cnt;i++)
{
int min1=INF,num=0;
for(int j=0;j<belong[i].size();j++)
{
int v=belong[i][j];
if(a[v]<min1){min1=a[v];num=0;}
if(a[v]==min1)num++;
}
//cout<<<<endl;
ans+=min1,p=p*num%MOD;
}
cout<<ans<<" "<<p<<endl;
return 0;
}

D. Match & Catch

Police headquarter is monitoring signal on different frequency levels. They have got two suspiciously encoded strings s1 and s2 from two different frequencies as signals. They are suspecting that these two strings are from two different criminals and they are planning to do some evil task.

Now they are trying to find a common substring of minimum length between these two strings. The substring must occur only once in the first string, and also it must occur only once in the second string.

Given two strings s1 and s2 consist of lowercase Latin letters, find the smallest (by length) common substring p of both s1 and s2, where p is a unique substring in s1 and also in s2. See notes for formal definition of substring and uniqueness.

Input

The first line of input contains s1 and the second line contains s2 (1 ≤ |s1|, |s2| ≤ 5000). Both strings consist of lowercase Latin letters.

Output

Print the length of the smallest common unique substring of s1 and s2. If there are no common unique substrings of s1 and s2 print -1.

Sample test(s)
Input
apple
pepperoni

Output
2

Input
lover
driver

Output
1

Input
bidhan
roy

Output
-1

Input
testsetses
teeptes

Output
3

Note

Imagine we have string a = a1a2a3...a|a|, where |a| is the length of string a, and ai is the ith letter of the string.

We will call string alal + 1al + 2...ar (1 ≤ l ≤ r ≤ |a|) the substring [l, r] of the string a.

The substring [l, r] is unique in a if and only if there is no pair l1, r1 such that l1 ≠ l and the substring [l1, r1] is equal to the substring [l, r] in a.

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF=1000000000;
const int maxn=10010;
string s1,s2;
int n,lens1;
int sa[maxn],t[maxn],t2[maxn],c[maxn];
void build_sa(int m)
{
int *x=t,*y=t2;
for(int i=0;i<m;i++)c[i]=0;
for(int i=0;i<n;i++)c[x[i]=s1[i]]++;
for(int i=1;i<m;i++)c[i]+=c[i-1];
for(int i=n-1;i>=0;i--)sa[--c[x[i]]]=i;
for(int k=1;k<=n;k<<=1)
{
int p=0;
for(int i=n-k;i<n;i++)y[p++]=i;
for(int i=0;i<n;i++)if(sa[i]>=k)y[p++]=sa[i]-k;
for(int i=0;i<m;i++)c[i]=0;
for(int i=0;i<n;i++)c[x[y[i]]]++;
for(int i=1;i<m;i++)c[i]+=c[i-1];
for(int i=n-1;i>=0;i--)sa[--c[x[y[i]]]]=y[i];
swap(x,y);
p=1;x[sa[0]]=0;
for(int i=1;i<n;i++)
x[sa[i]]=(y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k])?p-1:p++;
if(p>=n)break;
m=p;
}
}
int rank[maxn],h[maxn];
void getHeight()
{
int k=0;
for(int i=0;i<n;i++)rank[sa[i]]=i;
for(int i=0;i<n;i++)
{
if(k)k--;
int j=sa[rank[i]-1];
while(s1[i+k]==s1[j+k])k++;
h[rank[i]]=k;
}
}
void solve()
{
lens1=s1.size();
s1+='\$';
s1+=s2;
n=s1.size();
build_sa(123);
getHeight();
int ans=INF;
for(int i=2;i<n;i++)
{
if((sa[i]<lens1)!=(sa[i-1]<lens1))
{
if(h[i]>h[i-1]&&h[i]>h[i+1])
ans=min(ans,max(h[i+1],h[i-1])+1);
}
}
printf("%d\n",ans==INF?-1:ans);
}
int main()
{
//freopen("in.txt","r",stdin);
cin>>s1>>s2;
solve();
return 0;
}


E. Police Patrol

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1000010;
int n,m;
int a[maxn];
int main()
{

scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
long long ans=0;
int cnt=0,x=1,mid;
if(n%2)mid=n/2+1;
else mid=n/2;

for(int i=1;i<mid;i++)
{
cnt++;
if(cnt>=m){ans+=((long long)a[mid]-a[x])*2;cnt=0;x=i+1;}
}
if(cnt>0)ans+=((long long)a[mid]-a[x<1?1:x])*2;
cnt=0;
x=n;
for(int i=n;i>mid;i--)
{
cnt++;
if(cnt>=m){ans+=((long long)a[x]-a[mid])*2;cnt=0;x=i-1;}
}
if(cnt>0)ans+=((long long)a[x>n?n:x]-a[mid])*2;
printf("%I64d\n",ans);
//cout<<ans<<endl;
return 0;
}

#include<cstdio>
int a[1000010];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int p1=0,p2=n-1;
__int64 ans=0;
while(p2-p1>0)//不断逼近
{
ans=ans+a[p2]-a[p1];
p2=p2-m;//要想总距离最少，就必须先捉最左m个和最右m个
p1=p1+m;
}
ans=ans*2;
printf("%I64d\n",ans);
}
return 0;
}