7-1 办事大厅排队
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
string s,name;
cin>>n;
list<string> l;
while(n--){
cin>>s;
if(s=="in"){
cin>>name;
l.push_back(name);
}else if(s=="out"){
l.pop_front();
}else if(s=="q"){
if(l.empty()){
cout<<"NULL"<<endl;
}else{
cout<<l.front()<<endl;
}
}
}
return 0;
}
7-2 二叉搜索树的四种遍历!【模板】
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct Node{
int data;
Node *l;
Node *r;
} *root;
vector<int> v1, v2, v3, v4;//储存遍历信息,方便输出
void Creat(Node *&t, int d)
{
if(t == NULL){//找到空位置,插入
t = new Node;
t->data = d;
t->l = NULL;
t->r = NULL;
return;
}
if(d < t->data)//更小就往左边走
Creat(t->l, d);
else
Creat(t->r, d);
}
void pr(Node *t)//前序遍历
{
if(t){
v1.push_back(t->data);
pr(t->l);
pr(t->r);
}
}
void in(Node *t)//中序遍历
{
if(t){
in(t->l);
v2.push_back(t->data);
in(t->r);
}
}
void po(Node *t)//后序遍历
{
if(t){
po(t->l);
po(t->r);
v3.push_back(t->data);
}
}
void bfs(Node *t)//层序遍历
{
queue<Node*> q;
Node *p;
q.push(t);
while(q.size()){
Node *p = q.front();
q.pop();
v4.push_back(p->data);
if(p->l)
q.push(p->l);
if(p->r)
q.push(p->r);
}
}
void show(vector<int> v)
{
cout<<v[0];
for(int i = 1; i < v.size(); i++)
cout<<" "<<v[i];
cout<<endl;
}
int main()
{
int n, d;
cin>>n;
while(n--){
cin>>d;
Creat(root, d);
}
pr(root);
show(v1);
in(root);
show(v2);
po(root);
show(v3);
bfs(root);
show(v4);
return 0;
}
7-3 利用STL比较数据大小并排序
#include<bits/stdc++.h>
using namespace std;
int main(){
vector<int> data;
int n;
cout<<"从标准设备读入数据,直到输入是非整型数据为止"<<endl;
while(cin>>n){
data.push_back(n);
}
sort(data.begin(),data.end());
for(unsigned i=0;i<data.size();++i){
cout<<" "<<data[i];
}
return 0;
}
7-4 素数链表
#include<bits/stdc++.h>
using namespace std;
int n, m, k, l, r,q;
int sushu(int l, int r){
m = 0, k = 0;
while(l <= r)
{
int f = 1;
if(l == 1) f = 0;
for(int i = 2; i <= sqrt(l); i ++)
if(l % i == 0)
f = 0;
if(f){
k ++;
if(m ++ ) cout << ' ';
cout << l;
}
l ++;
}
if(!k)
cout << "none";
return 0;
}
int main()
{
cin >> n;
while(n --)
{
cin >> l >> r;
if(q++)
cout << endl;
sushu(l, r);
}
}
7-5 创建两个链表
#include<bits/stdc++.h>
using namespace std;
int m, n ,k,l;
string s;
int main()
{
cin >> n;
while(n --)
{
int jitop = 0, outop = 0;
int ji[1010]={0}, ou[1010]={0};
cin >> s;
for(int i = 0; i < s.size(); i ++)
{
if(i % 2 == 0) ji[jitop ++] = s[i] - '0';
else ou[outop ++] = s[i] - '0';
}
jitop --; outop --;
k = 0; l = 0;
while(jitop >= 0)
{
if(k ++) cout << ' ';
cout << ji[jitop --];
}
cout << endl;
for(int i = 0; i <= outop; i ++)
{
if(l ++) cout << ' ';
cout << ou[i];
}
cout << endl;
}
}
7-6 删除公共字符
#include <iostream>
#include <string>
// Function to remove characters from the first string that are present in the second string
std::string removeCommonCharacters(const std::string& str1, const std::string& str2) {
std::string result = "";
// Create a boolean array to store if a character is present in the second string
bool charPresent[256] = { false };
// Mark characters from the second string as present
for (char ch : str2) {
charPresent[ch] = true;
}
// Iterate through the first string and add characters that are not present in the second string to the result
for (char ch : str1) {
if (!charPresent[ch]) {
result += ch;
}
}
return result;
}
int main() {
std::string str1, str2;
while (std::getline(std::cin, str1)) {
std::getline(std::cin, str2);
std::string removedString = removeCommonCharacters(str1, str2);
std::cout << removedString << std::endl;
}
return 0;
}
7-7 寻找第k小的数
#include <iostream>
#include <algorithm>
void scan_d(int &num) {
char in;
in = getchar();
while (in < '0' || in > '9') in = getchar();
num = in - '0';
while (in = getchar(), in >= '0' && in <= '9') { num *= 10; num += in - '0'; }
}
int main() {
int n, k;
while (scanf("%d %d", &n, &k) != EOF) {
int* arr = new int[n];
for (int i = 0; i < n; ++i) {
scan_d(arr[i]);
}
std::nth_element(arr, arr + k - 1, arr + n);
std::cout << arr[k - 1] << std::endl;
delete[] arr;
}
return 0;
}
7-8 一个恐怖的寒胖子(注9分得7分的代码)
#include <iostream>
#include <vector>
// Function to compute the longest proper prefix which is also suffix for each substring
std::vector<int> computeLPS(const std::string& pattern) {
int m = pattern.size();
std::vector<int> lps(m);
int len = 0;
lps[0] = 0;
int i = 1;
while (i < m) {
if (pattern[i] == pattern[len]) {
len++;
lps[i] = len;
i++;
} else {
if (len != 0) {
len = lps[len - 1];
} else {
lps[i] = 0;
i++;
}
}
}
return lps;
}
// Function to find the first occurrence of pattern in text using KMP algorithm
int findPattern(const std::string& text, const std::string& pattern) {
int n = text.size();
int m = pattern.size();
std::vector<int> lps = computeLPS(pattern);
int i = 0; // Index for text
int j = 0; // Index for pattern
while (i < n) {
if (pattern[j] == text[i]) {
i++;
j++;
}
if (j == m) {
return i - j; // Found pattern at index i-j in text
} else if (i < n && pattern[j] != text[i]) {
if (j != 0) {
j = lps[j - 1];
} else {
i++;
}
}
}
return -1; // Pattern not found in text
}
int main() {
std::string text;
std::cin >> text;
std::string pattern = "hanpangzi";
int position = findPattern(text, pattern);
std::cout << position << std::endl;
return 0;
}