1.寻找子串
#include<bits/stdc++.h>
using namespace std;
const int N=110;
int main()
{
char str[N];
cin >> str;
/*strstr算法的时间复杂的为O(n)==kmp算法;
*/
if (strstr(str,"1111111") != NULL || strstr(str,"0000000") != NULL)
puts("YES");
else
puts("NO");
return 0;
}
关于strstr算法的具体细节在这-> 点这
主要注意内容:strstr(char str1[N],char str2[N]);
函数功能为:判断str2是否为str1的子串,若是则返回str1中第一个str2的首地址,否则返回NULL;
2.构造矩阵
这类问题属于构造类型的问题,亦属于听天由命的问题,考察临场应变的能力与智商的关系不大,做不出来也不要气馁
//两大步骤解决问题
1.只考虑(n-1) * (n-1)的区域 进行所谓的“全排列”
1 2 3 4 5
2 3 4 5 1
3 4 5 2 1
4 5 1 2 3
5 1 2 3 4
2.进行n*n的矩阵进行遍历并且对对角线的元素进行交换和赋值为0
0 2 3 4 5 1
2 0 4 5 1 3
3 4 0 1 2 5
4 5 1 0 3 2
5 1 2 3 0 4
1 3 5 2 4 0
#include<bits/stdc++.h>
using namespace std;
const int N=1010;
int n;
int chat[N][N];
// y=-x+n
int main(){
cin>>n;
for(int i=0;i<n-1;i++){
for(int j=0;j<n-1;j++){
chat[i][j]=(i+j)%(n-1)+1;
}
}
for(int i=0;i<n-1;i++){
for(int j=0;j<n-1;j++){
if(i==j){
chat[i][n-1]=chat[i][j];
chat[n-1][j]=chat[i][j];
chat[i][j]=0;
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<chat[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
错误思路:当n<1000时刻就应该放弃使用dfs而不是浪费大好时光(具体情况详见 点这)
常见错误板块
(1,0) 相当于 (0,0) 因此y+1==x; 即
表0 1 2 3 4 5
1
2
3
4
5
在图表题目中一般习惯以横轴为y,纵轴为x
如本题中的y=x为判断对角线的条件的前提下,很明显
x=1时,y=0,满足于同一尺度下的y=x的条件,因此现在的判定条件为y+1=x ,口诀为 小的加距