解题报告
--by zhangsheng
这次考试是在是太难了,其实我觉得每一道题都超过了普及组的范围。
T1:KMP
我就不讲了,其实暴力就可以得到70分,反正我们普及组应该不会狠心到让我们写这种东西。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <stack>
#include <iostream>
#include <ctype.h>
using namespace std;
const int Maxn=100000005;
int ans,len1,len2;
char s[100005],str[Maxn];
int f[Maxn];
inline void calc() {
int i=0,j=-1; f[i]=j;
while(i<len1) {
if(j==-1||s[i]==s[j]) i++,j++,f[i]=j;
else j=f[j];
}
}
inline void kmp() {
calc();
int i=0,j=0;
for(i=0;i<len2;i++) {
while(j&&str[i]!=s[j]) j=f[j];
if(str[i]==s[j]) j++;
if(j>=len1)ans++,j=f[j];
}
}
int main() {
scanf("%s%s",s,str);
len1=strlen(s),len2=strlen(str);
calc(); ans=0; kmp();
printf("%d
",ans);
return 0;
}
T2:就是一道小学奥数题,再加上高精度
这个坑点:两边都可以放。
所以我们可以的到每一次的分界点是sigma(3^i)这个数值,在套一个高精度的模板就可以了。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <stack>
#include <iostream>
#include <ctype.h>
#include <vector>
using namespace std;
inline int read() {
int w=0,x=0;char ch=0;
while (!isdigit(ch)) {w|=ch=='-';ch=getchar();}
while (isdigit(ch)) {x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
return w?-x:x;
}
struct BigInteger{
static const int base=100000000;
static const int Width=8;
vector<long long>s;
BigInteger() {*this=0;}
BigInteger(const int&num) {*this=num;}
BigInteger operator = (int num) {
s.clear();
do{
s.push_back(num%base);
num/=base;
}
while(num>0);
return *this;
}
BigInteger operator +(const BigInteger& b) {
BigInteger c;
c.s.clear();
for(int i=0,g=0;;i++) {
if(g==0&&i>=s.size()&&i>=b.s.size())break;
int x=g;
if(i<s.size())x+=s[i];
if(i<b.s.size())x+=b.s[i];
c.s.push_back(x%base);
g=x/base;
}
return c;
}
BigInteger operator =(const string& str) {
s.clear();
int x,len=(str.length()-1)/Width+1;
for (int i=0;i<len;i++){
int end=str.length()-i*Width;
int start=max(0,end-Width);
sscanf(str.substr(start,end-start).c_str(),"%lld",&x);
s.push_back(x);
}
return *this;
}
bool operator <(const BigInteger& b) {
if(s.size()<b.s.size())return true;
if(s.size()>b.s.size())return false;
for(int i=s.size()-1;i>=0;i--) {
if(s[i]<b.s[i])return true;
if(s[i]>b.s[i])return false;
}
return false;
}
bool operator == (const BigInteger& b) {
if(s.size()!=b.s.size())return false;
for(int i=0; i<s.size(); i++)
if(s[i]!=b.s[i])return false;
return true;
}
BigInteger operator *(const BigInteger& b) {
BigInteger c;
c.s.resize(s.size()+b.s.size());
for(int i=0; i<s.size(); i++)
for(int j=0; j<b.s.size(); j++)c.s[i+j]+=s[i]*b.s[j];
for(int i=0; i<c.s.size()-1; i++) {
c.s[i+1]+=c.s[i]/base;
c.s[i]%=base;
}
while(c.s.back()==0&&c.s.size()>1)c.s.pop_back();
return c;
}
friend istream& operator >>(istream& input,BigInteger& x) {
string s;
if(!(input>>s))return input;
x=s;
return input;
}
};
BigInteger Copy(const BigInteger& b,int x) {
BigInteger t;
t.s.resize(b.s.size()+x);
for(int i=0; i<b.s.size(); i++)t.s[i+x]=b.s[i];
return t;
}
BigInteger a,b,c,d;
int main() {
std::ios::sync_with_stdio(false);;
cin>>a; int ans=1;
b="1"; c="3"; d="1";
while (b<a) d=d*c,b=b+d,ans++;
printf("%d
",ans);
return 0;
}
T3:动态规划
Dp1[i][j]表示从(1,1)到(i,j)的路径条数
Dp2[i][j]表示从(i,j)到(n,m)的路径条数
Dp1[i][j]=dp1[i-1][j]+dp1[i][j-1]
Dp2[i][j]=dp2[i+1][j]+dp2[i][j+1]
(以上的两个方程如果不能通过那么就是0)
然后通过乘法原理在直接算出答案就可以了。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <stack>
#include <iostream>
#include <ctype.h>
using namespace std;
typedef long long LL;
const LL Mod=1e9+7;
int n,m,k;
LL Mp[2505][2505],dp1[2505][2505],dp2[2505][2505];
inline int read() {
int x=0,w=0; char ch=0;
while (!isdigit(ch)) {w|=ch=='-';ch=getchar();}
while (isdigit(ch)) {x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
return w?-x:x;
}
inline int Min(int n,int m) {return n<m?n:m;}
inline int Max(int n,int m) {return n>m?n:m;}
int main() {
n=read(),m=read(),k=read();
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++) scanf("%lld",&Mp[i][j]);
dp1[1][1]=1;
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++) {
if (i!=1 || j!=1) {
if (Mp[i][j]==0) dp1[i][j]=(dp1[i-1][j]+dp1[i][j-1])%Mod;
else dp1[i][j]=0;
}
}
dp2[n][m]=1;
for (int i=n;i>=1;i--)
for (int j=m;j>=1;j--) {
if (i!=n || j!=m) {
if (Mp[i][j]==0) dp2[i][j]=(dp2[i+1][j]+dp2[i][j+1])%Mod;
else dp2[i][j]=0;
}
}
while (k--) {
int x=read(),y=read();
printf("%d
",((dp1[1][1]*dp2[1][1])%Mod-(dp1[x][y]*dp2[x][y])%Mod+Mod)%Mod);
}
return 0;
}
T4:spfa
跑一遍spfa,求出最大的最小值。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <stack>
#include <iostream>
#include <ctype.h>
#include <queue>
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long LL;
const LL Inf=1<<30;
LL mp[2005][2005];
LL dist[2005];
int n,m,L;
bool vis[2005];
inline int read() {
int x=0,w=0; char ch=0;
while (!isdigit(ch)) {w|=ch=='-';ch=getchar();}
while (isdigit(ch)) {x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
return w?-x:x;
}
inline int Min(int n,int m) {return n<m?n:m;}
inline int Max(int n,int m) {return n>m?n:m;}
inline LL Max(LL n,LL m) {return n>m?n:m;}
inline LL Min(LL n,LL m) {return n<m?n:m;}
inline void Spfa(int s) {
queue<int>q;
for (int i=1;i<=n;i++) vis[i]=false,dist[i]=0;
q.push(s); vis[s]=true; dist[s]=Inf;
while (!q.empty()) {
int u=q.front(); q.pop(); vis[u]=false;
for (int i=1;i<=n;i++) {
if (mp[u][i]==0||u==i) continue;
if (Min(mp[u][i],dist[u])>dist[i]) {
dist[i]=Min(mp[u][i],dist[u]);
if (vis[i]==false) vis[i]=true,q.push(i);
}
}
}
}
int main() {
n=read(),m=read(),L=read();
for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) mp[i][j]=0;
for (int i=1;i<=m;i++) {
int x=read(),y=read(); LL z; scanf("%lld",&z);
if (x==y) continue;
mp[y][x]=mp[x][y]=Max(mp[x][y],z);
}
Spfa(1);
for (int i=2;i<=n;i++) printf("%lld
",dist[i]);
return 0;
}