-
常錯bug部分
i++??????
邏輯錯誤
分號出現(xiàn)在不該出現(xiàn)的位置
純粹手打快出錯
位置放錯
0或1值的處理遺漏惑芭,cout對位數(shù)的偷工減料
死循環(huán)啦
- 并查集中的father[i]=i初始化;
- union中father[t1]=t2;而不是t1=father[t2]
- 設立的標記數(shù)組更新還是不需要更新
- 函數(shù)中調(diào)用全局變量昵宇,全局變量已有初值梯醒,多加了一次
- isprime里的if(x<=1)return false;忘寫了
- isprime里的sqrt(1.0*x),x寫成n了,偷換變量
- in-off型存儲郑原,先排序唉韭,一個vector兩個兩個存夜涕,兩個兩個取
- 處理時間的問題,全化到最惺舴摺(一個標準)女器,大減小
- getline(cin,s)處理含空格的字符串時,如果前面出現(xiàn)換行住诸,要加一個getchar()
- 字符串cin驾胆,cout超時問題采用hash或換成scanf("%s",s.c_str)
- 處理字符串時,錯誤判斷數(shù)字的位數(shù)
- 在處理輸出保留幾位eg:%05d時贱呐,整體全用printf俏拱,防止遺漏。
- 分子為0的情況吼句?
- 因為除號取整锅必,先取整后乘還是先乘后取整~~~
- 數(shù)據(jù)中有出現(xiàn)double,可能另一種沒有說明的數(shù)據(jù)類型也是double
- 數(shù)據(jù)超過int型用longlong存儲
- 開數(shù)組時剛好開到邊界處會數(shù)組越界
- 有些畫圖題惕艳,空的地方必須是空格搞隐,否則一直0
-
題目:
搜索
two points
貪心
map映射
圖論
樹
模擬
排隊模擬
二分
其他
-
代碼塊
gcd
int gcd(int a,int b)
{
return !b ? a:gcd(b,a%b);
}
在一串字符串中提取單詞远搪。
//two points,第一個指針找第一個單詞劣纲,第二個指針找完這個單詞
vector<string>vt;
int main()
{
string s;
getline(cin,s);
int t;
for(int i=0;i<s.size();i++)
{
if((s[i]>='A'&&s[i]<='Z')||(s[i]>='a'&&s[i]<='z'))
{
t=i;
string temp="";
while((s[t]>='A'&&s[t]<='Z')||(s[t]>='a'&&s[t]<='z'))
{
temp+=s[t];
t++;
}
vt.push_back(temp);
i=t;
}
}
for(int i=0;i<vt.size();i++)
cout<<vt[i]<<endl;
return 0;
}
STL中非空判斷要在前,會出現(xiàn)段錯誤
1.一定要先檢測非空再判斷
while(!st.empty()&&st.top()==a[t])
// while(st.top()==a[t]&&!st.empty())
{
st.pop();
t++;
}
有些并查集的題不路徑壓縮過不了
int findfather(int x)
{
if(x==father[x])
return x;
else
{
int f=findfather(father[x]);
father[x]=f;
return f;
}
}
排隊問題的窗口處理
void deal(int pindex,int tindex)
{
if(person[pindex].arrive>=table[tindex].endtime)//來遲了
person[pindex].start=person[pindex].arrive;
else
person[pindex].start=table[tindex].endtime;谁鳍。//來早了
table[tindex].endtime=person[pindex].start+person[pindex].dotime;//窗口處理完的時間
}
最短路和最短路+dfs
- 點數(shù)范圍都較少癞季,使用鄰接矩陣存儲
- 著重點傾向于求最短路徑的第二標尺,求==輸出路徑滿足:最小/大點權(quán)倘潜,邊權(quán)绷柒,平均點權(quán),平均邊權(quán)==
- 方法 ==只用dijkstra==或==dijkstra+dfs==
1.只用dijkstra
順便求出第二標尺涮因,第三標尺的模板
//dis[]最短路徑
//G[][]邊權(quán)
//w[]點權(quán)和废睦,weight[]點權(quán)
//num[]最短路條數(shù)
//pt[]到該點的累計頂點數(shù)
//pre[]前驅(qū)結(jié)點
//注意各個數(shù)組的初始化
if(book[v]==0)//k未被訪問
{
if(dis[v]>dis[u]+G[u][v])
{
dis[v]=dis[u]+G[u][v];
w[v]=w[u]+weight[v];//加上v處的點權(quán)
pre[v]=u;
pt[v]=pt[u]+1//s->v的頂點數(shù)等于s->u的頂點數(shù)+1
num[v]=num[u];//更新到v點的最短路的條數(shù)
}
else if(dis[v]==dis[u]+G[u][v])
{
if(w[v]<w[u]+weight[v])
{
w[v]=w[u]+weight[v];
pt[v]=pt[u]+1;
pre[v]=u;
}
else if(w[v]==w[u]+weight[v])
{
........
}
num[k]+=num[u];//最短路的條數(shù)
}
}
dfs輸出路徑
void dfs(int v)
{
if(v==st)
{
printf("%d",v);
return ;
}
dfs(pre[v]);
printf("%d",v);
}
2.dijkstra+dfs
dis[]數(shù)組更新時
//
vector<int>pre[MAX];
//
if(dis[v]>dis[u]+G[u][v])
{
dis[v]=dis[u]+G[u][v];
pre[v].clear();//否定比它長的那一條
pre[v].push_back(u);
}
else if(dis[v]==dis[u]+G[u][v])
{
pre[v].push_back(u);
}
dfs模板
//
vector<int>path,temppath;
//
void dfs(int start)
{
if(v==start)
{
temppath.push_back(v);
.....
int value//用于計算臨時路徑temppath的第二標尺的值
.....
//計算時注意循環(huán)從temppath.size()-1開始,倒敘养泡。
.....
if(value 優(yōu)于 最佳value)
{
最佳value=value;
path=temppath;
}
temppath.pop_back();//回溯
return ;
}
temppath.push_back(v);
for(int i=0;i<pre[v].size();i++)
{
dfs(pre[v][i]);
}
temppath.pop_back();//回溯
}
分塊
int SQR=633;//633塊嗜湃,632個
int block[SQR];
int table[MAX];
void fenkuai(int k)
{
int sum=0;
int index=0;
while(sum+block[index]<k)
{
sum+=block[index++];
}
int num=index*632;
while(sum+table[num]<k)
{
sum+=table[num++];
}
cout<<num<<endl;
}
BST
struct node
{
node* lchild;
node* rchild;
int data;
};
node* newnode(int x)
{
node* Node=new node;
Node->data=x;
Node->lchild=Node->rchild=NULL;
return Node;
}
//初始化 node* root=NULL;
//////////////////////////
void Insert(node* &root,int data)
{
if(root==NULL)
{
root=newnode(data);
return ;
}
if(data<root->data)
Insert(root->lchild,data);
else
Insert(root->rchild,data);
}
///////////////////////////////