SDUT数据结构PTA专题(实验四)题解
·
数据结构与算法A实验四串、数组、广义表
7-1 【模板】KMP字符串匹配 (20 分)
#include<bits/stdc++.h>
#define ll long long
const int N = 1e5 + 10;
using namespace std;
int ne[N]; // 前缀数组
string s,st; // s母串,st模式串
void get_next(){ //求模式串的Next数组
int i=0,j=-1;
ne[0]=-1;
int len=st.size();
while(i<len){
if(j==-1||st[i]==st[j]) ne[++i]=++j;
else j=ne[j];
}
}
void Kmp(){ // kmp计算
int i=0,j=0;
while(i<s.size()){
if(j==-1||s[i]==st[j]) i++,j++;
else j=ne[j];
if(j==st.size()) cout<<i-j+1<<endl; // 输出模式串出现的位置
}
// if(j>=st.size()) cout<<i-j+1<<endl;
// else cout<<-1<<endl;
}
inline void solve(){
cin>>s>>st;
get_next();
Kmp();
for(int i=0;i<st.size();i++){ // 输出next数组 从1开始输出
if(i) cout<<" ";
cout<<ne[i+1];
}
cout<<endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
7-2 串的模式匹配 (25 分)
#include<bits/stdc++.h>
#define ll long long
const int N = 1e5 + 10;
using namespace std;
int ne[N]; // 前缀数组
string s,st; // s母串,st模式串
void get_next(){ //求模式串的Next数组
int i=0,j=-1;
ne[0]=-1;
int len=st.size();
while(i<len){
if(j==-1||st[i]==st[j]) ne[++i]=++j;
else j=ne[j];
}
}
int Kmp(){ // kmp计算
int i=0,j=0;
while(i<s.size()){
if(j==-1||s[i]==st[j]) i++,j++;
else j=ne[j];
if(j==st.size()) return i-j+1; // 输出模式串出现的位置
}
return -1;
}
inline void solve(){
cin>>s;
int q;
cin>>q;
while(q--){
cin>>st;
get_next();
int tag=Kmp();
if(tag!=-1){ // 如果匹配成功 输出匹配成功位置后面的字符串
for(int i=tag-1;i<s.size();i++) cout<<s[i];
cout<<endl;
}else cout<<"Not Found"<<endl; // 匹配不成功 输出Not Found
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
7-3 字符串模式匹配 (5 分)
#include<bits/stdc++.h>
#define ll long long
const int N = 1e5 + 10;
using namespace std;
int ne[N]; // 前缀数组
string s,st; // s母串,st模式串
void get_next(){ //求模式串的Next数组
int i=0,j=-1;
ne[0]=-1;
int len=st.size();
while(i<len){
if(j==-1||st[i]==st[j]) ne[++i]=++j;
else j=ne[j];
}
for(int i=0;i<(int)st.size();i++){ // 输出next数组
cout<<ne[i+1]-1<<" ";
}
cout<<endl;
}
void Kmp(){ // KMP操作
int i=0,j=0;
while(i<(int)s.size()&&j<(int)st.size()){
if(j==-1||s[i]==st[j]) i++,j++;
else j=ne[j];
}
if(j>=(int)st.size()) cout<<i-j<<endl;
else cout<<-1<<endl;
}
inline void solve(){
cin>>s>>st;
get_next();
Kmp();
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
7-4 好中缀 (10 分)
#include<bits/stdc++.h>
#define ll long long
const int N = 1e5 + 10;
using namespace std;
int ne[N]; // 前缀数组
string s,st; // s母串,st模式串
void get_next(){ //求模式串的Next数组
int i=0,j=-1;
ne[0]=-1;
int len=st.size();
while(i<len){
if(j==-1||st[i]==st[j]) ne[++i]=++j;
else j=ne[j];
}
}
void Kmp(){
int i=0,j=0;
while(i<(int)s.size()&&j<(int)st.size()){
if(j==-1||s[i]==st[j]) i++,j++;
else j=ne[j];
}
if(j>=(int)st.size()) cout<<i-j<<endl;
else cout<<-1<<endl;
}
int res[N];
inline void solve(){
cin>>st;
get_next();
//Kmp();
int j=ne[(int)st.size()];
int pos=0;
while(j>0) res[pos++]=j,j=ne[j];
sort(res,res+(int)st.size(),greater<int>());
printf("%d\n",max(0,(int)st.size()-res[1]*2)); // 输出答案
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
7-5 三元组顺序表表示的稀疏矩阵转置Ⅱ (10 分)
#include<bits/stdc++.h>
#define ll long long
#define eb emplace_back
#define PII pair<int,int>
#define x first
#define y second
const int N = 1e5 + 10;
using namespace std;
struct node{ // 存数据
int x,y,k,id;
int p;
}dp[N];
bool cmp(node x,node y){ // 转置之后排序规则
if(x.x<y.x) return 1;
else if(x.x==y.x&&x.y<y.y) return 1;
return 0;
}
bool ord(node x,node y){ // 回归排序前的转置规则
return x.id<y.id;
}
inline void solve(){
int n,m,t;
cin>>n>>m>>t;
int x,y,k;
for(int i=1;i<=t;i++){
cin>>x>>y>>k;
dp[i]={y,x,k,i,0}; // 直接将 xy交换实现转置
}
sort(dp+1,dp+1+t,cmp);
for(int i=1;i<=t;i++) dp[i].p=i-1; // 排序后的顺序
sort(dp+1,dp+1+t,ord);
for(int i=1;i<=t;i++) cout<<dp[i].p<<" "<<dp[i].x<<" "<<dp[i].y<<" "<<dp[i].k<<endl; // 按照输入顺序输出
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
更多推荐
所有评论(0)