0
刘乐宸
新手天翼
新手天翼
先道个歉,那个代码并不是纯正的dfs,而是结合了bfs的部分队列内容,再加上一些无意义的累加,以完成a+b的效果。切勿模仿学习。
但是,对于队列,你们可能误解了一些。我只是调用了队首元素,而并非用了下表(只是用push_back和pop更新位置)。
但是说实话恶搞a+b还挺好玩的😀
所以,不定期分享一些代码。希望你们能够喜欢!
如下:
刘乐宸在2021-08-05 18:58:42追加了内容
递推写法:
#include<bits/stdc++.h>
using name** std;
int a,b;
const int maxn=105;
int sum[maxn];
int main()
{
cin>>a>>b;
sum[0]=a;
for(int i=1;i<=b;i++)
sum[i]=1+sum[i-1];
cout<<sum[b];
ret**n 0;
}
刘乐宸在2021-08-05 19:05:52追加了内容
#include <bits/stdc++.h>
using name** std;
const int maxn=204;
int a[maxn],b[maxn],c[maxn];
st**ng Add(st**ng as,st**ng bs) {
int al=as.size(), bl=bs.size();
int cl=max(al,bl)+1;
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
memset(c, 0, sizeof(c));
for(int i=1; i<=al; i++)
a[i] = as[al-i]-'0';
for(int i=1; i<=bl; i++)
b[i] = bs[bl-i]-'0';
for(int i=1; i<=cl; i++) {
c[i]+=a[i]+b[i];
c[i+1]=c[i]/10;
c[i]%=10;
}
st**ng **;
while(c[cl]==0 && cl>1) cl--;
for(int i=cl; i>=1; i--)
**+= c[i]+'0';
ret**n **;
}
int main() {
st**ng x, y;
cin >> x >> y;
cout << Add(x,y);
ret**n 0;
}
高精度
刘乐宸在2021-08-05 21:19:44追加了内容
递归
#include <bits/stdc++.h>
using name** std;
int akman(int a, int b) {
int ans=0;
if(a==0) ret**n ans+b;
ret**n ans+1+akman(a-1, b);
}
int main() {
int a,b;
cin>>a>>b;
int ans=akman(a, b);
cout<<ans;
ret**n 0;
}
刘乐宸在2021-08-05 21:21:14追加了内容
二叉堆
#include<bits/stdc++.h>
using name** std;
int n,m;
p**o**ty_queue<int>q;
void tree(int a,int b)
{
for(int i=a;i<=b+1;i++)
q.push(i);
}
int main()
{
int a,b;
cin>>n>>m;
tree(n,m);
cout<<q.top();
ret**n 0;
}
刘乐宸在2021-08-13 23:14:05追加了内容
后缀树(转自洛谷)
#include<bits/stdc++.h>
#define inf 1e9
#define eps 1e-6
#define N 100010
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline ll read()
{
char ch=getchar();
ll s=0,w=1;
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){s=s*10+ch-'0';ch=getchar();}
return s*w;
}
ll n,m;
struct edge
{
ll next,to;
}e[N<<1];
ll head[N],cnt,vis[N];
ll pos1[N],pos2[N];
ll a[N],s[N];
char s1[N],s2[N];
ll size[N],C[N],p[N],ans;
ll minn=inf,rt,block;
struct SAM
{
ll len[N],ch[N][26],fa[N];
ll tot,son[N][26],R[N],size[N],num[N],last;
ll p[N],c[N];
ll s[N];
SAM(){tot=last=1;}
inline void insert(ll x)
{
ll nowp=++tot,p=last;len[nowp]=len[p]+1;size[nowp]=1;R[nowp]=len[nowp];
while(p&&!ch[p][x])ch[p][x]=nowp,p=fa[p];
if(!p)fa[nowp]=1;
else
{
ll q=ch[p][x];
if(len[q]==len[p]+1)fa[nowp]=q;
else
{
ll nowq=++tot;len[nowq]=len[p]+1;
fa[nowq]=fa[q];fa[q]=nowq,fa[nowp]=nowq;
for(register ll i=0;i<26;i++)ch[nowq][i]=ch[q][i];
while(p&&ch[p][x]==q)ch[p][x]=nowq,p=fa[p];
}
}
last=nowp;
}//建SAM
inline void build()
{
for(register ll i=1;i<=tot;i++)c[len[i]]++;
for(register ll i=1;i<=m;i++)c[i]+=c[i-1];
for(register ll i=1;i<=tot;i++)p[c[len[i]]--]=i;
for(register ll i=tot;i>=2;i--)
{
ll x=p[i];
size[fa[x]]+=size[x];R[fa[x]]=R[x];
son[fa[x]][s[R[x]-len[fa[x]]]]=x;
}
}
inline void clear(){for(register ll i=1;i<=tot;i++)num[i]=0;}
inline void calc(ll now,ll father,ll p,ll L)
{
if(len[p]==L)p=son[p][a[now]];
else if(s[R[p]-L]!=a[now])p=0;
if(!p)return ;//T不在S直接return
num[p]++;
for(register ll i=head[now];i;i=e[i].next)
{
if(e[i].to==father||vis[e[i].to])continue;
calc(e[i].to,now,p,L+1);
}
}
inline void pushdown()
{
for(register ll i=2;i<=tot;i++)
{
ll x=p[i];
num[x]+=num[fa[x]];
}
}
}S1,S2;
inline void add_edge(ll from,ll to){e[++cnt]=(edge){head[from],to};head[from]=cnt;}
void getroot(ll now,ll Ns,ll father)
{
size[now]=1;ll maxn=-inf;
for(register ll i=head[now];i;i=e[i].next)
{
if(e[i].to==father||vis[e[i].to])continue;
getroot(e[i].to,Ns,now);size[now]+=size[e[i].to];
maxn=max(maxn,size[e[i].to]);
}
maxn=max(maxn,Ns-size[now]);
if(maxn<minn)minn=maxn,rt=now;
}
void dfs1(ll now,ll father)
{
p[++p[0]]=now;
for(register ll i=head[now];i;i=e[i].next)
{
if(e[i].to==father||vis[e[i].to])continue;
dfs1(e[i].to,now);
}
}
void dfs2(ll now,ll father,ll x)
{
x=S1.ch[x][a[now]];if(!x)return ;
ans+=S1.size[x];
for(register ll i=head[now];i;i=e[i].next)
{
if(e[i].to==father||vis[e[i].to])continue;
dfs2(e[i].to,now,x);
}
}
void calc(ll x,ll father,ll f)
{
S1.clear(),S2.clear();
if(father)
{
S1.calc(x,0,S1.son[1][a[father]],1);
S2.calc(x,0,S2.son[1][a[father]],1);
}
else {S1.calc(x,0,1,0);S2.calc(x,0,1,0);}
S1.pushdown();S2.pushdown();
for(register ll i=1;i<=m;i++)
{
ans+=f*S1.num[pos1[i]]*S2.num[pos2[m-i+1]];
}
}
void DFS(ll now,ll father)
{
C[now]=1;
for(register ll i=head[now];i;i=e[i].next)
{
if(e[i].to==father||vis[e[i].to])continue;
DFS(e[i].to,now);C[now]+=C[e[i].to];
}
}
void dfs(ll now,ll Ns)
{
if(Ns<=block)
{
p[0]=0;dfs1(now,0);
for(register ll i=1;i<=p[0];i++)dfs2(p[i],0,1);
return ;
}
vis[now]=1;calc(now,0,1);DFS(now,0);
for(register ll i=head[now];i;i=e[i].next)
{
if(vis[e[i].to])continue;
calc(e[i].to,now,-1);
minn=inf,rt=0;
getroot(e[i].to,C[e[i].to],0);dfs(rt,C[e[i].to]);
}
}
int main()
{
n=read(),m=read();block=sqrt(n);
for(register ll i=1;i<n;i++)
{
ll x=read(),y=read();
add_edge(x,y);add_edge(y,x);
}
scanf("%s",s1+1);
for(register ll i=1;i<=n;i++)a[i]=s1[i]-'a';
for(register ll i=1;i<=m;i++)s[i]=s2[i]-'a';
for(register ll i=1;i<=m;i++)S1.insert(s[i]),pos1[i]=S1.last,S1.s[i]=s[i];
reverse(s+1,s+m+1);
for(register ll i=1;i<=m;i++)S2.insert(s[i]),pos2[i]=S2.last,S2.s[i]=s[i];
S1.build();S2.build();
getroot(1,n,0);dfs(rt,n);
printf("%lld\n",ans);
return 0;
}
刘乐宸在2021-08-14 16:16:40追加了内容
再淼个1000
#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#define panduan_w 3
using namespace std;
const int MAXN = 10;
string t = "Hello, World!";//题目要求输出的
string ans = "Hello, World!";//对照组
int ms, p=0, fflag = 0, pd, fei;
struct F{
char ml;
bool used;
}px[30*MAXN];
bool check(int n) {
for(int i=1; i<=n; i++) {
if(ans[i] != t[i]) return 1;//判断对照组与实际是否相同
}
return 0;
}
void ensh(int sea) {
if(sea == 0) sea = 0;
if(sea == 1) {
cout << "I can't do this problem!It's so difficult!";
fei = 1;
}
}
bool ff(int du) {
pd = du;
pd -= 1;
du = pd;
if(du != 0) return 1;
return 0;
}
void date(int a, int b) {
if(check(ms)!= 1 && p==0 && ff(fflag)==0 && a+b==2 && fei == 0)
cout << "Hello, World!";
else if(fei == 1) cout << "I can't do this problem!It's so difficult!";
}
bool wcheck(string x,string y) {
if(x==y) return true;
}
int main() {
string ans = "Hello, World!";//对照组
ms = ans.size();
int kk = ms;//寄存
string t = "Hello, World!";//题目要求输出的
check(ms);
int ss = 0;//判断双方长度是否相同
do{
ss++;
}
while(ms --);
ms = kk;
if(ss == (ms-1)) {
p = MAXN;
}
char sh[15][15];//绘制string类型表格
int ii = sqrt(ms);
int jj = kk - ii;
for(int i=1; i<=ii; i++) {
for(int j=1; j<=kk; j++) {
sh[i][j] = t[j];
}
}
for(int i=1; i<=kk; i++) {
if(sh[1][i] == t[i]) {
fflag = 1;
}
}
for(int i=0; i<kk; i++) {
px[i].ml = t[i];
px[i].used = 0;
}
for(int i=0; i<kk; i++){
if(px[i].ml == t[i]) px[i].used = 1;
}
for(int i=0; i<kk; i++) {
if(px[i].used == 0)fei = 1;
}
ensh(fei);
char aa = ' ', bb = 'H';//判断首字母
if(ans[0] == aa && ans[1] !=bb) {
cout << "I can't do this problem!It's so difficult!";
while(true) aa = '0';
return 0;
}
date(p+fei+1, fflag);
//添加后置函数判断w区别
string wq=t;
string wdz[panduan_w]={"word","would"};
/* for(int i=0;i<=3;i++) {
if(wcheck(wq,wdz[i])==true){
cout<<"\nWait,it's different!";
return 0;
}
}*/
return 0;
}
同学写的,无偿搬运
刘乐宸在2021-08-14 16:16:47追加了内容
再淼个1000
#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#define panduan_w 3
using namespace std;
const int MAXN = 10;
string t = "Hello, World!";//题目要求输出的
string ans = "Hello, World!";//对照组
int ms, p=0, fflag = 0, pd, fei;
struct F{
char ml;
bool used;
}px[30*MAXN];
bool check(int n) {
for(int i=1; i<=n; i++) {
if(ans[i] != t[i]) return 1;//判断对照组与实际是否相同
}
return 0;
}
void ensh(int sea) {
if(sea == 0) sea = 0;
if(sea == 1) {
cout << "I can't do this problem!It's so difficult!";
fei = 1;
}
}
bool ff(int du) {
pd = du;
pd -= 1;
du = pd;
if(du != 0) return 1;
return 0;
}
void date(int a, int b) {
if(check(ms)!= 1 && p==0 && ff(fflag)==0 && a+b==2 && fei == 0)
cout << "Hello, World!";
else if(fei == 1) cout << "I can't do this problem!It's so difficult!";
}
bool wcheck(string x,string y) {
if(x==y) return true;
}
int main() {
string ans = "Hello, World!";//对照组
ms = ans.size();
int kk = ms;//寄存
string t = "Hello, World!";//题目要求输出的
check(ms);
int ss = 0;//判断双方长度是否相同
do{
ss++;
}
while(ms --);
ms = kk;
if(ss == (ms-1)) {
p = MAXN;
}
char sh[15][15];//绘制string类型表格
int ii = sqrt(ms);
int jj = kk - ii;
for(int i=1; i<=ii; i++) {
for(int j=1; j<=kk; j++) {
sh[i][j] = t[j];
}
}
for(int i=1; i<=kk; i++) {
if(sh[1][i] == t[i]) {
fflag = 1;
}
}
for(int i=0; i<kk; i++) {
px[i].ml = t[i];
px[i].used = 0;
}
for(int i=0; i<kk; i++){
if(px[i].ml == t[i]) px[i].used = 1;
}
for(int i=0; i<kk; i++) {
if(px[i].used == 0)fei = 1;
}
ensh(fei);
char aa = ' ', bb = 'H';//判断首字母
if(ans[0] == aa && ans[1] !=bb) {
cout << "I can't do this problem!It's so difficult!";
while(true) aa = '0';
return 0;
}
date(p+fei+1, fflag);
//添加后置函数判断w区别
string wq=t;
string wdz[panduan_w]={"word","would"};
/* for(int i=0;i<=3;i++) {
if(wcheck(wq,wdz[i])==true){
cout<<"\nWait,it's different!";
return 0;
}
}*/
return 0;
}
同学写的,无偿搬运
0
0
0
0
0
0
0
0
0
0
0
李奕歌
初级天翼
初级天翼
高精度
#include<iostream>
#include<algo**thm>
using name** std;
int main()
{
st**ng a,b;
long long xa[100000]={},xb[100000]={},tot[100000]={};
cin>>a>>b;
for(int i=0;i<a.len**();i++)
xa[i]=a[a.len**()-i-1]-'0';
for(int i=0;i<b.len**();i++)
xb[i]=b[b.len**()-i-1]-'0';
int len=max(a.len**(),b.len**());
for(int i=0;i<len;i++)
tot[i]=xa[i]+xb[i];
for(int i=0;i<len;i++)
{
tot[i+1]+=tot[i]/10;
tot[i]%=10;
}
if(tot[len]) cout<<tot[len];
for(int i=len-1;i>=0;i--)
cout<<tot[i];
cout<<endl;
ret**n 0;
}
0
0