//统计回文子串
#include <iostream>
#include <string>
using namespace std;
int countPalindromeSubstrings(const string& s) {
int count = 0;
int n = s.length();
// 遍历字符串的每个字符作为回文中心
for (int center = 0; center < n; center++) {
// 情况1:以当前字符为回文中心的回文字符子串长度为奇数
int left = center;
int right = center;
while (left >= 0 && right < n && s[left] == s[right]) {
count++;
left--;
right++;
}
// 情况2:以当前字符及其右边字符为回文中心的回文字符子串长度为偶数
left = center;
right = center + 1;
while (left >= 0 && right < n && s[left] == s[right]) {
count++;
left--;
right++;
}
}
return count;
}
int main() {
string s;
cout << "Enter a string: ";
cin >> s;
int count = countPalindromeSubstrings(s);
cout << "Number of palindrome substrings: " << count << endl;
system("pause");
return 0;
}
//公约数
#include <iostream>
using namespace std;
int work(int a, int b)
{
if (b == 0)
return a;
return work(b, a % b);
}
int process(int n)
{
int count = 0;
for (int i = 1; i < n; i++)
{
if (work(i, n) == 1)
count++;
}
return count;
}
int main()
{
int cn;
cin >>cn;
for (int i = 0; i < cn; i++)
{
int n;
cin >> n;
cout << process(n) << endl;
}
system("pause");
return 0;
}
//约瑟夫
#include <iostream>
#include <queue>
using namespace std;
int main()
{
int n, m;
cin >> n >> m;
queue<int> q;
for (int i = 1; i <= n; i++)
{
q.push(i);
}
int c = 0;
while (q.size()>1){
c++;
if(c%m==0)q.pop();
else{
q.push(q.front());
q.pop();
}
}
cout<<q.front();
system("pause");
return 0;
}
//进制
#include <iostream>
#include <stack>
#include<string>
using namespace std;
void work(int n,int r,stack<int>&s)
{
while(n>0){
s.push(n%r);
n=n/r;
}
}
int main()
{
int n,r;
cin>>n>>r;
string letters = "ABCDEFGHIGKLMNOPQRSTUVWXYZ";
stack<int> s;
work(n,r,s);
while(!s.empty()){
if(s.top()<10)cout<<s.top();
else cout<<letters[s.top()-10];
s.pop();
}
system("pause");
return 0;
}
//波兰
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Solution {
public:
int evalRPN(vector<string>& tokens) {
string token = tokens.back();
tokens.pop_back();
if (token == "+" || token == "-" || token == "*" || token == "/") {
int operand2 = evalRPN(tokens);
int operand1 = evalRPN(tokens);
if (token == "+") {
return operand1 + operand2;
} else if (token == "-") {
return operand1 - operand2;
} else if (token == "*") {
return operand1 * operand2;
} else if (token == "/") {
return operand1 / operand2;
}
} else {
return stoi(token);
}
return 0;
}
};
int main(){
vector<string> tokens = {"2", "1", "+", "3", "*"};
Solution s;
cout<<s.evalRPN(tokens)<<endl;
system("pause");
return 0;
}
//合并队列
#include <iostream>
using namespace std;
int main() {
int N;
cin >> N;
int a[20000], b[200000];
for (int i = 0; i < N; i++) {
cin >> a[i];
}
for (int i = 0; i < N; i++) {
cin >> b[i];
}
int pa = 0, pb = 0;
while (pa < N && pb < N) {
if (a[pa] < b[pb]) {
cout << a[pa++] << " ";
} else {
cout << b[pb++] << " ";
}
}
if (pa + pb != 2 * N) {
if (pa == N) {
for (int i = pb; i < N; i++) {
cout << b[i] << " ";
}
} else {
for (int i = pa; i < N; i++) {
cout << a[i] << " ";
}
}
}
return 0;
}
//子串个数
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
while (cin >> s) {
int len = s.size();
cout << len * (len + 1) / 2 + 1 << "\n";
}
return 0;
}
//模式串
#include <iostream>
#include <string>
using namespace std;
int findPattern(const string& target, const string& pattern) {
int n = target.length();
int m = pattern.length();
for (int i = 0; i <= n - m; i++) {
int j;
for (j = 0; j < m; j++) {
if (target[i + j] != pattern[j]) {
break;
}
}
if (j == m) {
return i + 1;
}
}
return 0;
}
int main() {
string target, pattern;
getline(cin, target);
getline(cin, pattern);
int position = findPattern(target, pattern);
cout << position << endl;
return 0;
}
//螺旋
#include <iostream>
#include <vector>
using namespace std;
void generateSpiralMatrix(int N) {
vector<vector<int>> matrix(N, vector<int>(N));
int num = 1;
int topRow = 0, bottomRow = N - 1, leftCol = 0, rightCol = N - 1;
while (num <= N * N) {
for (int i = leftCol; i <= rightCol; i++) {
matrix[topRow][i] = num++;
}
topRow++;
for (int i = topRow; i <= bottomRow; i++) {
matrix[i][rightCol] = num++;
}
rightCol--;
for (int i = rightCol; i >= leftCol; i--) {
matrix[bottomRow][i] = num++;
}
bottomRow--;
for (int i = bottomRow; i >= topRow; i--) {
matrix[i][leftCol] = num++;
}
leftCol++;
}
// Output the spiral matrix
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
int main() {
int N;
cin >> N;
generateSpiralMatrix(N);
return 0;
}
//汉诺塔
#include <iostream>
using namespace std;
void moveDisk(int n, char source, char destination)
{
cout << "Move disk " << n << " from " << source << " to " << destination << endl;
}
void hanoi(int n, char source, char auxiliary, char destination)
{
if (n == 1)
{
moveDisk(n, source, destination);
}
else
{
hanoi(n - 1, source, destination, auxiliary);
moveDisk(n, source, destination);
hanoi(n - 1, auxiliary, source, destination);
}
}
int main()
{
int numDisks;
cout << "Enter the number of disks: ";
cin >> numDisks;
hanoi(numDisks, 'A', 'B', 'C');
return 0;
}
//dfs
#include<bits/stdc++.h>
using namespace std;
int mp[101][101];
bool f[101]={0};
int i,n,m;
void dfs(int x)
{
cout<<x<<" ";
for(int i=1;i<=m;i++)
{
if(mp[x][i]&&f[i]==0)
{
f[i]=1;
dfs(i);
}
}
}
int main()
{
// int m,n;
cin>>m>>n;
int a,b;
for(int i=1;i<=n;i++)
{
cin>>a>>b;
mp[a][b]=1;
mp[b][a]=1;
}
dfs(0);
}
//堆
#include<bits/stdc++.h>
using namespace std;
void xgd(int b[],int k,int x)
{
b[0]=b[k];
for(int i=2*k;i<=x;i=i*2)
{
if(i<x&&b[i]>b[i+1])
{
i++;
}
if(b[0]<=b[i]) break;
else{
b[k]=b[i];
k=i;
}}
b[k]=b[0];
}
void creat(int b[],int l)
{
for(int i=l/2;i>0;i--)
{
xgd(b,i,l);
}
}
int main()
{
int n;
cin>>n;
int l=0;
int a[n];
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
creat(a,n);
for(int i=1;i<=n;i++)
{
cout<<a[i]<<" ";
}
}
// 查找次数
#include<bits/stdc++.h>
using namespace std;
int n;
int a[1001];
int f(int x)
{
int t=n,m=0,r=1,sum=0,f=0;
while(t>=r)
{
m=((t-r)/2)+r;
if(a[m]>x)
{
t=m-1;
if(t!=r)
sum++;
}
else if(a[m]<x){
r=m+1;
if(t!=r)
sum++;
}
else{
sum++;
return sum;
}
}
return -1;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
int x,p=0;
cin>>x;
p=f(x);
if(p==-1) cout<<"NO";
else cout<<p;
}
//map
#include<vector>
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
struct stu
{
int learn_num;
string name;
int seat_num;
};
int main()
{
vector<stu>vec;
stu a;
int n, learn_num, seat_num, index;
string name;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> learn_num;
a.learn_num = learn_num;
cin >> name;
a.name = name;
cin >> seat_num;
a.seat_num = seat_num;
vec.push_back(a);
}
cin >> index;
for (int i = 0; i < n; i++)
{
if (vec[i].learn_num == index)
{
cout << vec[i].name << " ";
cout << vec[i].seat_num << endl;
break;
}
}
}
//最小生成树
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct edge
{
int weight;
edge(int weight) : weight(weight) {}
};
void PrimMST(vector<vector<edge>> &g)
{
vector<int> distance(g.size(), INT_MAX);
vector<bool> inMST(g.size(),0);
for (int i = 1; i < g.size(); i++)
{
if (g[0][i].weight != 0)
distance[i] = g[0][i].weight;
}
distance[0] = 0;
inMST[0]=true;
for (int i = 1; i < g.size(); i++)
{
int mindis = INT_MAX;
int minindex = -1;
for (int j = 0; j < g.size(); j++)
{
if (!inMST[j] && distance[j] < mindis)
{
mindis = distance[j];
minindex = j;
}
}
inMST[minindex] = true;
for (int j = 0; j < g.size(); j++)
{
if (!inMST[j] && g[minindex][j].weight != 0 && g[minindex][j].weight < distance[j])
distance[j] = g[minindex][j].weight;
}
}
int sum = 0;
for (int i = 0; i < g.size(); i++)
{
sum += distance[i];
}
cout << sum <<endl;
}
int main()
{
int n;
cin >> n;
vector<vector<edge>> graph(n);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
int weight;
cin >> weight;
graph[i].push_back(edge(weight));
}
}
PrimMST(graph);
system("pause");
return 0;
}
// 最短路径
#include <iostream>
using namespace std;
const int MAXV = 100; // 最大顶点数
const int INF = 9999; // 表示无穷大的值
class MatGraph {
public:
int n; // 顶点数量
int edges[MAXV][MAXV]; // 邻接矩阵
MatGraph(int numVertices) {
n = numVertices;
initializeGraph();
}
void addEdge(int from, int to, int weight) {
if (from >= 0 && from < n && to >= 0 && to < n) {
edges[from][to] = weight;
}
}
private:
void initializeGraph() {
for (int i = 0; i < MAXV; i++) {
for (int j = 0; j < MAXV; j++) {
edges[i][j] = 0; // 初始化邻接矩阵,所有元素置为0
}
}
}
};
void Dijkstra(MatGraph& g, int v) {
int dist[MAXV];
int path[MAXV];
int S[MAXV];
for (int i = 0; i < g.n; i++) {
dist[i] = g.edges[v][i]; // 距离初始化
S[i] = 0; // 将S置空
if (g.edges[v][i] != 0 && g.edges[v][i] < INF) {
path[i] = v; // 当v到i有边时,置i的前驱顶点为v
}
else {
path[i] = -1; // 当v到i没边时,置i的前驱顶点为-1
}
}
S[v] = 1; // 将源点编号v放入S中
int mindis, u = -1;
for (int i = 0; i < g.n - 1; i++) {
mindis = INF; // mindis置最小距离初值
for (int j = 0; j < g.n; j++) {
if (S[j] == 0 && dist[j] < mindis) {
u = j;
mindis = dist[j];
}
}
S[u] = 1; // 将顶点u加入S中
for (int j = 0; j < g.n; j++) {
if (S[j] == 0) {
if (g.edges[u][j] < INF && dist[u] + g.edges[u][j] < dist[j]) {
dist[j] = dist[u] + g.edges[u][j];
path[j] = u;
}
}
}
}
}
int main() {
return 0;
}
//快速排序
#include<iostream>
#include<vector>
using namespace std;
void quicksort(vector<int> &v,int i,int j){
if(i>=j)return;
int left = i,right = j;
int key = left;
while(left<right){
while(v[right]>=v[key]&&left<right){
right--;
}
while(v[left]<=v[key]&&left<right){
left++;
}
swap(v[left],v[right]);
}
swap(v[key],v[left]);
quicksort(v,i,left-1);
quicksort(v,left+1,j);
}
void quicksort1(vector<int> &v,int i,int j){
if(i>=j)return;
int left=i,right=j;
int key=left;
int tmp=v[key];
while(left<right){
while(v[right]>=tmp&&left<right){
right--;
}
v[key]=v[right];
key=right;
while(v[left]<=tmp&&left<right){
left++;
}
v[key]=v[left];
key=left;
}
v[left]=tmp;
quicksort1(v,i,left-1);
quicksort(v,left+1,j);
}
int main(){
vector<int> v = {5,6,3,5,8,9};
quicksort1(v,0,v.size()-1);
for(auto it:v){
cout<<it<<" ";
}
system("pause");
return 0;
}
//生成二叉树
#include<iostream>
#include<string>
using namespace std;
struct BTNode{
char data;
BTNode *lchild,*rchild;
BTNode(char x):data(x),lchild(NULL),rchild(NULL){}
};
BTNode *build11(string pre, int i, string mid, int j, int n)
{
if(n<=0)return NULL;
char root_data = pre[i];
BTNode *root = new BTNode(root_data);
int p = j;
while(mid[p]!=root_data)p++;
int num = p-j;
root->lchild = build11(pre, i+1, mid, j, num);
root->rchild = build11(pre, i+num+1, mid, p+1, n-num-1);
return root;
}
BTNode *build21(string &in,int i,string &post,int j,int n){
if(n<=0)return NULL;
char data = post[j+n-1];
int p=i;
while(in[p]!=data)p++;
int num = p-i;
BTNode *root = new BTNode(data);
root->lchild = build21(in,i,post,j,num);
root->rchild = build21(in,p+1,post,j+num,n-num-1);
return root;
}
void display(BTNode *root){
if(root==NULL)return;
cout<<root->data;
display(root->lchild);
display(root->rchild);
}
int main(){
string in,post;
char order;
cin>>in>>post>>order;
BTNode *root=build21(in,0,post,0,in.size());
display(root);
system("pause");
return 0;
}
// 根据前序和后序构建二叉树
#include <iostream>
#include <vector>
using namespace std;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(NULL), right(NULL) {}
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
void display(TreeNode *root)
{
if (root == NULL)
return;
display(root->left);
cout << root->val << ',';
display(root->right);
}
class Solution
{
public:
TreeNode *constructFromPrePost(vector<int> &preorder, vector<int> &postorder)
{
return helper(preorder, 0, postorder, 0, preorder.size());
}
TreeNode *helper(vector<int> &preorder, int i, vector<int> &postorder, int j, int n)
{
if (n <= 0)
return NULL;
TreeNode *node = new TreeNode(preorder[i]);
int index = j;
while (postorder[index] != preorder[i + 1])
{
index++;
}
node->left = helper(preorder, i + 1, postorder, j, index - j);
node->right = helper(preorder, i + index - j + 1, postorder, index + 1, n - index- j - 1);
return node;
}
};
int main()
{
vector<int> preorder = {1, 2, 4, 5, 3, 6, 7};
vector<int> postorder = {4, 5, 2, 6, 7, 3, 1};
Solution s;
TreeNode *root = s.constructFromPrePost(preorder, postorder);
display(root);
system("pause");
return 0;
}