offer66

有序:二分
串变长:先定长
二叉树:三种遍历,记录路径,深搜时要善于利用返回值(一般不让另外开数组维护)
栈与队列:根据所求开辅助栈
数组寻找而不消耗额外空间:把值取反,或者加上最大值令超限
(即用1变-1,或26个字母的话我就加26,以此表示已访问,且可以获取到原值)
二进制下从低位起逐个1消去:n&=(n-1)
二进制下获取最低位1的位置:n&(-n)
链表:同步指针保持差不变,快针&慢针可以求差值
数组找出次数有关的数字:阵地攻守
数组找连续区间或找两个值:尺取法(即左右针)
字符串计数/全排:逐位深搜
实在没有头绪,可以考虑位操作

……跟ACM有很大的不同就是,不会让你打表,不会让你开很大的数组……
 

下面是今天手撕的模板,有些用不着,有备无患吧

还要补充:多线程

//KMP
#include
using namespace std;
int slen,tlen,s[103],t[103],nxt[103];
void getnxt(){
    int k=-1,j=0;
    nxt[0]=-1;
    while(j>cas;
    while(cas--){
        cin>>slen>>tlen;
        for(int i=0;i>s[i];
        for(int i=0;i>t[i];
        int ans=kmp_match();
        if(ans==-1)cout<
using namespace std;
int n,tre[103];
void shift_down(int pos){
    int flag=0,nxt;
    while(pos*2<=n&&flag==0){
        nxt=pos;
        if(tre[pos]>tre[pos*2])
            nxt=pos*2;
        if(tre[nxt]>tre[nxt+1])
            nxt=nxt+1;
        if(nxt!=pos){
            int tmp=tre[pos];
            tre[pos]=tre[nxt];
            tre[nxt]=tmp;
            pos=nxt;
        }
        else{
            flag=1;
        }
    }
}
void heap_push(int x){
    n++;
    tre[n]=x;
    int p=n;
    while(tre[p/2]>tre[p]){
        swap(tre[p/2],tre[p]);
        p/=2;
    }
}
int heap_pop(){
    int tmp=tre[1];
    tre[1]=tre[n--];
    shift_down(1);
    return tmp;
}
int main(){
    heap_push(2);heap_push(4);heap_push(3);heap_push(1);heap_push(5);
    cout<
using namespace std;
int tre[1003*4];
int query(int l,int r,int id,int x,int y){
    int ans=0;
    if(x<=l&&r<=y){
        ans+=tre[id];
        return ans;
    }
    int mid=(l+r)/2;
    if(x<=mid)ans+=query(l,mid,id*2,x,y);
    if(y>mid)ans+=query(mid+1,r,id*2+1,x,y);
    return ans;
}
void update(int l,int r,int id,int x,int y){
    if(l>=r){
        tre[id]+=y;
        return ;
    }
    int mid=(l+r)/2;
    if(mid>=x)update(l,mid,id*2,x,y);
    else update(mid+1,r,id*2+1,x,y);
    tre[id]=tre[id*2]+tre[id*2+1];
}
int main(){
    update(1,10,1,2,2);
    update(1,10,1,3,5);
    update(1,10,1,6,7);
    cout<
using namespace std;
int main(){
    int b[10]={1,1,2,2,3,4,5,5,6,7};
    int n=unique(b,b+10)-b;
    for(int i=0;i
using namespace std;
int main(){
    vectora={1,1,2,2,3,3,4,4,5,6,7};
    a.erase(unique(a.begin(),a.end()),a.end());
    for(vector::iterator ite=a.begin();ite!=a.end();ite++)
        cout<<(*ite)<
using namespace std;
int floyd[103][103];
int main(){
    for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
            cin>>floyd[i][j];
    for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
            for(int k=0;k<3;k++)
                if(floyd[i][j]>floyd[i][k]+floyd[k][j])
                    floyd[i][j]=floyd[i][k]+floyd[k][j];
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++)
            cout<
using namespace std;
struct edge{
    int to;
    int cost;
};
vectorg[103];
typedef pair p;
int d[103];
void addedge(int from,int to,int cost){
    g[from].push_back(edge{to,cost});
    g[to].push_back(edge{from,cost});
}
void dij(int s){
    priority_queue,greater

>pq; pq.push(p(0,s)); memset(d,100,sizeof(d)); d[1]=0; while(!pq.empty()){ p pp=pq.top();pq.pop(); int nowp=pp.second; if(d[nowp]>pp.first)continue; for(int i=0;id[nowp]+cst){ d[newp]=d[nowp]+cst; pq.push(p(d[newp],newp)); } } } } int main(){ addedge(1,2,3);addedge(1,3,5);addedge(1,4,6); addedge(2,3,1);addedge(2,4,3);addedge(3,4,1); dij(1); cout< using namespace std; struct edge{int to;int cost;}; vectorg[103]; typedef pair p; void addedge(int from,int to,int cost){ g[from].push_back(edge{to,cost}); g[to].push_back(edge{from,cost}); } int fa[103],ans; void prim(int s){ priority_queue,greater

>pq; pq.push(p(0,s)); while(!pq.empty()){ //cout< using namespace std; struct edge{int from;int to;int cost;}; vectorg; int fa[103],ans; int fid(int x){ if(x==fa[x])return x; else return fa[x]=fid(fa[x]); } bool cmp(edge a,edge b){ if(a.cost using namespace std; int sum=0; bool check(char s[],int l,int r){ for(int i=l;i>s; int len=strlen(s); perm(s,0,len-1); cout< using namespace std; double f(double x){ return x*x+9*x+10; } int main(){ double l=-10000,r=10000; while(r-l>1e-3){ double x=l+(r-l)/3; double y=l+(r-l)/3*2; if(f(x)>f(y))l=x; else r=y; } cout< using namespace std; double f(double x){ return x*3; } int main(){ double l=-987,r=345; while(r-l>1e-3){ double mid=(l+r)/2; if(f(mid)>0)r=mid; else l=mid; } cout< using namespace std; int vis[10000],pri[10000]; int main(){ int d=sqrt(10000.0); for(int i=3;i>n; cout< using namespace std; int ksm(int x,int y){ int ans=1; while(y>0){ if(y&1)ans*=x; y>>=1; x*=x; } return ans; } int main(){ int x,y;cin>>x>>y; cout< using namespace std; struct mat{int m[103][103];}unit; int n,p; mat operator*(mat a,mat b){ mat ans; for(int i=0;i>=1; } return ans; } int main(){ for(int i=0;i<100;i++)unit.m[i][i]=1; cin>>n>>p; mat a; for(int i=0;i>a.m[i][j]; a=pos_mat(a,p); for(int i=0;i using namespace std; void merge_sort(int a[],int l,int r){ if(l>=r)return ; int mid=(l+r)/2; merge_sort(a,l,mid); merge_sort(a,mid+1,r); int tmp[10],k=l,i=l,j=mid+1; while(i<=mid&&j<=r){ if(a[i] using namespace std; struct node{ int cnt; struct node* nxt[26]; node(){ cnt=0; for(int i=0;i<26;i++) nxt[i]=NULL; } }; node* root; char str[103][30]; void maketrie(char *s){ node* p=root; node* tmp=NULL; for(int i=0;inxt[s[i]-'a']==NULL){ tmp=new node; p->nxt[s[i]-'a']=tmp; } p=p->nxt[s[i]-'a']; p->cnt++; } } void search(char *s){ node *p=root; for(int i=0;inxt[s[i]-'a']; cout<cnt==1)break; } } int main(){ root=new node; int n;cin>>n; for(int i=0;i>str[i]; maketrie(str[i]); } for(int i=0;i

 

你可能感兴趣的:(剑指OFFER66题)