@KirinBill
2017-10-09T15:32:59.000000Z
字数 4378
阅读 1262
题解
套题
赛后AK,心态爆炸。
目录
#include <cstdio>
#include <cctype>
#include <string>
using std::string;
inline void setIO(string file){
string in=file+".in",out=file+".out";
freopen(in.c_str(),"r",stdin);
freopen(out.c_str(),"w",stdout);
}
template<typename type>
inline void read(type &x){
int pm=1; char c;
do{
c=getchar();
if(c=='-') pm=-1;
}while(!isdigit(c));
x=c^'0';
while(c=getchar(),isdigit(c))
x=x*10+(c^'0');
x*=pm;
}
template<typename type>
void write(type x,char c=0){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10|'0');
if(c) putchar(c);
}
#include <algorithm>
using std::max;
const int MAXN=305;
int n,k;
char s[MAXN],t[MAXN];
inline int solve(int sl,int tl){
int rest=k,ret=0;
for(int i=sl,j=tl;i<=n && j<=n;++i,++j){
if(s[i]!=t[j]){
if(!rest) break;
else --rest;
}
++ret;
}
return ret;
}
int main(){
#ifdef DEBUG
setIO("a");
#endif
read(n),read(k);
scanf("%s%s",s+1,t+1);
int ans=0;
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
ans=max(ans,solve(i,j));
}
}
write(ans);
return 0;
}
bitset
优化的话,复杂度就除了个神奇的东西,可以过了。
#include <cstdio>
#include <cctype>
#include <string>
using std::string;
inline void setIO(string file){
string in=file+".in",out=file+".out";
freopen(in.c_str(),"r",stdin);
freopen(out.c_str(),"w",stdout);
}
template<typename type>
inline void read(type &x){
int pm=1; char c;
do{
c=getchar();
if(c=='-') pm=-1;
}while(!isdigit(c));
x=c^'0';
while(c=getchar(),isdigit(c))
x=x*10+(c^'0');
x*=pm;
}
template<typename type>
void write(type x,char c=0){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10|'0');
if(c) putchar(c);
}
#include <bitset>
using std::bitset;
const int MAXN=1505;
int deg[MAXN];
char s[MAXN];
bitset<MAXN> G[MAXN],tmp;
int main(){
#ifdef DEBUG
setIO("b");
#endif
int n;
read(n);
for(int i=1;i<=n;++i){
scanf("%s",s+1);
for(int j=1;j<=n;++j)
G[i][j]=s[j]^'0';
}
for(int i=1;i<=n;++i)
deg[i]=G[i].count();
long long ans=0;
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
if(i==j || !G[i][j]) continue;
ans+=(long long)(deg[i]-1)*(deg[j]-1);
tmp=G[i]&G[j];
ans-=tmp.count();
}
}
write(ans);
return 0;
}
#include <cstdio>
#include <cctype>
#include <string>
using std::string;
inline void setIO(string file){
string in=file+".in",out=file+".out";
freopen(in.c_str(),"r",stdin);
freopen(out.c_str(),"w",stdout);
}
template<typename type>
inline void read(type &x){
int pm=1; char c;
do{
c=getchar();
if(c=='-') pm=-1;
}while(!isdigit(c));
x=c^'0';
while(c=getchar(),isdigit(c))
x=x*10+(c^'0');
x*=pm;
}
template<typename type>
void write(type x,char c=0){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10|'0');
if(c) putchar(c);
}
#include <queue>
#include <vector>
#include <functional>
#include <cstring>
using std::priority_queue;
using std::vector;
using std::greater;
const int MAXN=200005,MAXM=300005,MAXW=1<<20,MAXV=MAXN+MAXW,MAXE=MAXM+(MAXN<<1);
int n,m;
int he[MAXV],dis[MAXV];
struct line{int to,nex,w;}ed[MAXE];
struct node{
int id;
node(int id=0):id(id){}
friend bool operator> (const node &a,const node &b){
return dis[a.id]>dis[b.id];
}
};
inline void addE(int u,int v,int w){
static int cnt=0;
ed[++cnt]=(line){v,he[u],w};
he[u]=cnt;
}
inline void hp_SPFA(){
static bool inQ[MAXV];
static priority_queue<node,vector<node>,greater<node> > pq;
memset(dis,0x7f,sizeof(dis));
dis[1]=0;
pq.push(node(1));
inQ[1]=true;
int u;
while(pq.size()){
u=pq.top().id;
pq.pop();
inQ[u]=false;
for(int i=he[u],v,d;i;i=ed[i].nex){
v=ed[i].to,d=dis[u]+ed[i].w;
if(dis[v]>d){
dis[v]=d;
if(!inQ[v]){
pq.push(node(v));
inQ[v]=true;
}
}
}
if(u>n){
for(int j=0,v,w=u-n,d=dis[u];(1<<j)<w;++j){
if(w&1<<j){
v=(w^1<<j)+n;
if(dis[v]>d){
dis[v]=d;
if(!inQ[v]){
pq.push(node(v));
inQ[v]=true;
}
}
}
}
}
}
}
int main(){
#ifdef DEBUG
setIO("c");
#endif
read(n),read(m);
for(int i=1,w,v;i<=n;++i){
read(w),v=n+w;
addE(i,v,1),addE(v,i,0);
}
for(int i=1,u,v;i<=m;++i){
read(u),read(v);
addE(u,v,1);
}
hp_SPFA();
for(int i=1;i<=n;++i){
if(dis[i]>n) dis[i]=-1;
write(dis[i],'\n');
}
return 0;
}