这一次的题目比28,26难了很多。
JZOJ NO.1【2014.10.18普及组】反射
分析
首先这道题我是栈溢出。
所以深搜我改为while (true){ }
代码
#include <cstdio>
using namespace std;
int n,ans,m; char a[1001][1001];
int main(){
freopen("mirror.in","r",stdin);
freopen("mirror.out","w",stdout);
scanf("%d%d\n",&n,&m);
for (int i=1;i<=n;i++) scanf("%s",a[i]+1);
for (int i=1;i<=n;i+=n-1)
for (int j=1;j<=m;j++){
int x=i,y=j,flag=(i==1)?1:3,manx=0;
while (true){
manx++;
switch (a[x][y]){
case '/':switch(flag){
case 1:y--,flag=4; break;
case 2:x--,flag=3; break;
case 3:y++,flag=2; break;
case 4:x++,flag=1; break;
} break;
case 92:switch(flag){
case 1:y++,flag=2; break;
case 2:x++,flag=1; break;
case 3:y--,flag=4; break;
case 4:x--,flag=3; break;
} break;
}
if (x<1||y<1||x>n||y>m) break;
}
ans=(ans>manx)?ans:manx;
}
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j+=m-1){
int x=i,y=j,flag=(j==1)?2:4,manx=0;
while (true){
manx++;
switch (a[x][y]){
case '/':switch(flag){
case 1:y--,flag=4; break;
case 2:x--,flag=3; break;
case 3:y++,flag=2; break;
case 4:x++,flag=1; break;
} break;
case 92:switch(flag){
case 1:y++,flag=2; break;
case 2:x++,flag=1; break;
case 3:y--,flag=4; break;
case 4:x--,flag=3; break;
} break;
}
if (x<1||y<1||x>n||y>m) break;
}
ans=(ans>manx)?ans:manx;
}
printf("%d",ans);
return 0;
}
JZOJ NO.2 【2014.10.18普及组】自动匹配
题目分析:
快排+二分
代码
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
struct q{
string s; int rank;
}a[60001];
int n,m,k; string st;
bool cmp(q x,q y){return x.s<y.s;}
int main(){
freopen("auto.in","r",stdin);
freopen("auto.out","w",stdout);
cin>>m>>n;
for (int i=1;i<=m;i++) cin>>a[i].s,a[i].rank=i;
sort(a+1,a+1+m,cmp);
for (int i=1;i<=n;i++){
cin>>k>>st;
int l=1,r=m;
while (l<=r){
int mid=(l+r)>>1;
if (a[mid].s<st) l=mid+1;
else r=mid-1;
}
if (!(a[l+k-1].s).find(st,0)) cout<<a[l+k-1].rank<<endl; else cout<<-1<<endl;
}
return 0;
}
JZOJ NO.3 1922. 【2014.10.18普及组】道路阻塞
分析
这道题可以用spfa。
代码
#include <cstdio>
#include <algorithm>
#include <climits>
#define b INT_MAX
using namespace std;
int stat[25001],low[251],f[501][501],g[501][501],n,m,x,y,sum,ans,quo;
int spfa(){
int head=0,tail=1; bool v[251]={0,1}; stat[1]=1;
for (int i=1;i<=n;i++) low[i]=b; low[1]=0;
do{
head=head%m+1;
int x=stat[head]; v[x]=1;
for (int i=1;i<=g[x][0];i++){
int c=g[x][i]; int w=f[x][c];
if (low[x]+w<low[c]){
low[c]=low[x]+w;
if (!v[c]){
tail=tail%m+1;
stat[tail]=c;
v[c]=1;
}
}
}
v[x]=0;
}while (head!=tail);
return low[n];
}
int main(){
freopen("rblock.in","r",stdin);
freopen("rblock.out","w",stdout);
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++) f[i][j]=b;
for (int i=1;i<=m;i++){
scanf("%d%d",&x,&y);
scanf("%d",&f[x][y]);
f[y][x]=f[x][y];
g[x][++g[x][0]]=y;
g[y][++g[y][0]]=x;
}
sum=spfa();
for (int i=1;i<=n;i++)
for (int j=i;j<=n;j++){
f[i][j]=f[j][i]=f[i][j]<<1;//设置障碍
int quo=spfa();
ans=max(ans,quo-sum);
f[i][j]=f[j][i]=f[i][j]>>1;//删除障碍
}
printf("%d",ans);
return 0;
}
JZOJ NO.4 【2014.10.18普及组】密码编码
代码(深搜)
#include <iostream>
#include <cstdio>
#include <map>
#define ansum ans=(ans+1)%2014
using namespace std;
string s; int ans,len;
map <string,int> uk;
int dfs(string s){
if (uk[s]) return uk[s];//有值说明计算过了
int ans=0,len=s.size();
for (int i=0;(i+1)*2<len;i++){
string a=s.substr(0,i+1);
string b=s.substr(i+1,i+1);
string c=s.substr(len-i-1,i+1);
string d=s.substr(len-i-i-2,i+1);
string e=s.substr(i+1,len-i-1);
string f=s.substr(0,len-i-1);
if (a==b) ans+=dfs(e);
if (a==c) ans+=dfs(e);
if (c==d) ans+=dfs(f);
if (c==a) ans+=dfs(f);
}
if (len>=2) ans++;//说明原串是一种方案
ans%=2014;
uk[s]+=ans;//赋值
return ans;
}
int main(){
freopen("scode.in","r",stdin);
freopen("scode.out","w",stdout);
cin>>s;
cout<<dfs(s)-1;//减去原串
}