#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define xx first
#define yy second
#define endl '\n'
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;
可以只写成
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
7-1 字符串模式匹配(KMP)
给定一个字符串 text 和一个模式串 pattern,求 pattern 在text 中的出现次数。text 和 pattern 中的字符均为英语大写字母或小写字母。text中不同位置出现的pattern 可重叠。
输入格式:
输入共两行,分别是字符串text 和模式串pattern。
输出格式:
输出一个整数,表示 pattern 在 text 中的出现次数。
输入样例1:
zyzyzyz
zyz
输出样例1:
3
输入样例2:
AABAACAADAABAABA
AABA
输出样例2:
3
数据范围与提示:
1≤text, pattern 的长度 ≤106, text、pattern 仅包含大小写字母。
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define xx first
#define yy second
#define endl '\n'
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;
int Next[N];
string a, b;
void dp()
{
int i = 0, j = -1;
int len = b.size();
Next[0] = -1;
while(i < len)
{
if(j == -1 || b[i] == b[j])
{
i ++;
j ++;
Next[i] = j;
}
else j = Next[j];
}
}
int kmp()
{
int n1 = a.size(), n2 = b.size();
int i = 0, j = 0;
int cnt = 0;
while(i < n1 && j < n2)
{
if(j == -1 || a[i] == b[j])
{
i ++;
j ++;
}
else j = Next[j];
if(j == n2)
{
cnt ++;
j = Next[j];
}
}
return cnt;
}
int main()
{
cin >> a >> b;
dp();
cout << kmp() << endl;
return 0;
}
//aabaabfgghaabaacgyg
7-2 【模板】KMP字符串匹配
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define xx first
#define yy second
#define endl '\n'
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;
string s, t;
int Next[N];
void dp()
{
int n = t.size();
int i = 0, j = -1;
Next[0] = -1;
while(i < n)
{
if(j == -1 || t[i] == t[j])
{
i ++;
j ++;
Next[i] = j;
}
else j = Next[j];
}
}
void kmp()
{
int n1 = s.size(), n2 = t.size();
int i = 0, j = 0;
while(i < n1 && j < n2)
{
if(j == -1 || s[i] == t[j])
{
i ++;
j ++;
}
else j = Next[j];
if(j == n2)
{
cout << i - j + 1 << endl;
j = Next[j];
}
}
}
int main()
{
cin >> s >> t;
dp();
kmp();
int n = t.size();
for(int i = 1; i <= n; i ++)
{
cout << Next[i] << " \n"[i == n];
}
return 0;
}
7-3 统计子串
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define xx first
#define yy second
#define endl '\n'
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;
string s, t;
int Next[N];
void dp()
{
int n = t.size();
int i = 0, j = -1;
Next[0] = -1;
while(i < n)
{
if(j == -1 || t[i] == t[j])
{
i ++;
j ++;
Next[i] = j;
}
else j = Next[j];
}
}
int cnt = 0, k;
void kmp()
{
int n1 = s.size(), n2 = t.size();
int i = 0, j = 0;
while(i < n1 && j < n2)
{
if(j == -1 || s[i] == t[j])
{
i ++;
j ++;
}
else j = Next[j];
if(j == n2)
{
cnt ++;
if(k == 0) k = i - j + 1;
i --;
j = Next[j - 1];
}
}
if(cnt == 0) cout << "0 0\n";
else cout << k << " " << cnt << endl;
}
int main()
{
int n;
cin >> n;
while(n --)
{
cin >> s >> t;
cnt = 0;
k = 0;
dp();
kmp();
}
return 0;
}
7-4 好中缀
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define xx first
#define yy second
#define endl '\n'
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;
string s, t;
int Next[N];
int ans[N];
void dp()
{
int n = t.size();
int i = 0, j = -1;
Next[0] = -1;
while(i < n)
{
if(j == -1 || t[i] == t[j])
{
i ++;
j ++;
Next[i] = j;
}
else j = Next[j];
}
}
int main()
{
cin >> t;
dp();
int n = t.size();
int i = Next[n];
int j = 0;
while(i > 0)
{
ans[j ++] = i;
i = Next[i];
}
sort(ans, ans + n, greater<int>());
n = n - ans[1]*2;
cout << max(0, n) << endl;
return 0;
}
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define xx first
#define yy second
#define endl '\n'
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;
char p[N];
int Next[N];
int ans[N];
string t;
void dp()
{
int n = t.size();
int i = 0, j = -1;
Next[0] = -1;
while(i < n)
{
if(j == -1 || t[i] == t[j])
{
i ++;
j ++;
Next[i] = j;
}
else j = Next[j];
}
}
int main()
{
cin>>t;
dp();
int n=t.size();
int x=n-2*Next[Next[n]];
if(Next[n] == -1 || Next[Next[n]] == -1)
cout << n << endl;
else cout<<max(0, x)<<endl;
return 0;
}
7-5 病毒变种
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define xx first
#define yy second
#define endl '\n'
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;
int main()
{
int t;
cin >> t;
while(t --)
{
string s;
cin >> s;
int n = s.size();
char c;
for(int i = 0; i < n; i ++)
{
cout << s << " ";
c = s[0];
for(int j = 1; j < n; j ++)
{
s[j - 1] = s[j];
}
s[n - 1] = c;
}
cout << endl;
}
return 0;
}
7-6 判断对称矩阵
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define xx first
#define yy second
#define endl '\n'
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e2 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;
int a[N][N];
int main()
{
int n;
cin >> n;
for(int i = 1; i <= n; i ++)
{
for(int j = 1; j <= n; j ++)
{
cin >> a[i][j];
}
}
int f = 0;
for(int i = 1; i <= n; i ++)
{
for(int j = i + 1; j <= n; j ++)
{
if(a[i][j] != a[j][i])
{
f = 1;
}
}
}
if(f) cout << "No\n";
else cout << "Yes\n";
return 0;
}
7-7 三元组顺序表表示的稀疏矩阵转置运算Ⅰ
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
struct node
{
int x, y, w;
}p[N];
bool cmp(node x, node y)
{
if (x.x != y.x) return x.x < y.x;
return x.y < y.y;
}
int main()
{
int n, m, t;
cin >> n >> m >> t;
// 直接 x 和 y 交换输入, 相当于转置后了
for (int i = 0; i < t; i++)
cin >> p[i].y >> p[i].x >> p[i].w;
sort(p, p + t, cmp);
for (int i = 0; i < t; i++)
cout << p[i].x << " " << p[i].y << " " << p[i].w << endl;
return 0;
}
7-8 三元组顺序表表示的稀疏矩阵加法
#include<bits/stdc++.h>
using namespace std;
map<pair<int, int>, int> mp;
int main()
{
int n, m, t1, t2;
cin >> n >> m >> t1 >> t2;
int x, y, w;
for (int i = 0; i < t1; i++)
{
cin >> x >> y >> w;
mp[{x, y}] += w;
}
for (int i = 0; i < t2; i++)
{
cin >> x >> y >> w;
mp[{x, y}] += w;
}
int cnt = 0;
for (auto it : mp)
{
if (it.second) cnt++;
}
cout << n << " " << m << " " << cnt << endl;
for (auto it : mp)
{
if (it.second) cout << it.first.first << " " << it.first.second << " " << it.second << endl;
}
return 0;
}
7-9 三元组顺序表表示的稀疏矩阵转置Ⅱ
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
struct node {
int x, y, w;
} p[N];
map<pair<int, int>, int> mp;
vector<node> v;
bool cmp(node a, node b) {
if (a.x != b.x) return a.x < b.x;
return a.y < b.y;
}
int main() {
int n, m, t;
cin >> n >> m >> t;
for (int i = 0; i < t; i++) cin >> p[i].y >> p[i].x >> p[i].w;
for (int i = 0; i < t; i++) v.push_back({p[i].x, p[i].y});
sort(v.begin(), v.end(), cmp);
for (int i = 0; i < t; i++) mp[{v[i].x, v[i].y}] = i;
for (int i = 0; i < t; i++) {
int x = p[i].x, y = p[i].y, w = p[i].w;
cout << mp[{x, y}] << " " << x << " " << y << " " << w << endl;
}
return 0;
}
7-10 最大子矩阵和问题
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
int a[N][N];
int d[N][N];
int t[N];
int n, m;
int x, y, xx, yy, ty, tyy;
int f()
{
int Max = 0, sum = t[1];
int l = 1, r = 1;
for (int i = 2; i <= m; i++)
{
if (sum > 0)
{
sum += t[i];
r = i;
}
else
{
sum = t[i];
l = r = i;
}
if (sum > Max)
{
Max = sum;
ty = l;
tyy = r;
}
}
if (sum > Max)
{
Max = sum;
ty = l;
tyy = r;
}
return Max;
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
scanf("%d", &a[i][j]);
d[i][j] = d[i - 1][j] + a[i][j];
}
}
int Max = 0;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j <= n; j++)
{
for (int k = 1; k <= m; k++)
t[k] = d[j][k] - d[i][k];
int maxn = f();
if (maxn > Max)
{
Max = maxn;
x = i + 1;
xx = j;
y = ty;
yy = tyy;
}
}
}
printf("%d\n", Max);
printf("%d %d %d %d\n", x, xx, y, yy);
return 0;
}