0
点赞
收藏
分享

微信扫一扫

第6周实验(等值子串,KMP模式匹配,大整数相乘, 最长公共子串,判断两个字符串是否匹配,最长回文子串,年号字串,公共钥匙盒——队列)

zhongjh 2022-03-30 阅读 42

1.等值子串

#include<bits/stdc++.h>
using namespace std;
int main(){
	string c;
	cin >> c;
	int s = 0, num = 0, tmp = 0, res = 1;
	int len = c.length();
	for(int i = 1; i < len; i++){
		if(c[i] == c[i - 1]) res++;
		else {
			if(res > num) {
				num = res;
				s = tmp;
			}
			tmp = i;
			res = 1;
		}
	}
	if(num > 1)
		for(int i = s; i < s + num; i++){
			cout << c[i];
		}
	else cout << "no";
}

 2.KMP模式匹配

#include<bits/stdc++.h>

const int N = 1e3 + 10;
int next[N], f[N];
int main() {
	std::string a, b;
	while(std::cin >> b >> a){
		int n = a.length(),m = b.length();
		a = " " + a; b = " " + b;
		for (int i = 2, j = 0; i <= n; i++) {
			while(j > 0 && a[i] != a[i + 1]) j = next[j];
			if(a[i] == a[j + 1]) j++;
			next[i] = j;
		}
		int ans = 0;
		for(int i = 1, j = 0; i <= m; i++) {
			while(j > 0 && (j == n || b[i] != a[j + 1])) j = next[j];
			if(b[i] == a[j + 1]) j++;
			f[i] = j;
			if(f[i] == n) ans = i;
		}
		if(ans != 0) std::cout << ans - n + 1 << std::endl;
		else std::cout << ans << std::endl;
	}
}


 3.大整数相乘

#include<bits/stdc++.h> 
using namespace std;
int main() {
	string aa, bb;
	int a[10001], b[10001], ans[10001] = { 0 };
	cin >> aa >> bb;
	int len1 = aa.length();
	int len2 = bb.length();
	for (int i = 0; i <= len1 - 1; i++) a[i] = aa[len1 - 1 - i] - '0';
	for (int i = 0; i <= len2 - 1; i++) b[i] = bb[len2 - 1 - i] - '0';
	for (int i = 0; i < len1; i++) {
		for (int j = 0; j < len2; j++) {
			ans[i + j] += a[i] * b[j];
		}
	}
	for (int i = 0; i < len1 + len2 - 1; i++) {
		if (ans[i] > 9) {
			ans[i + 1] += ans[i] / 10;
			ans[i] %= 10;
		}
	}
	int len = len1 + len2-1;
	while (ans[len]==0 && len > 0) len--;
	for (int i = len; i >= 0; i--) cout << ans[i];
}

 4.最长公共子串

#include<bits/stdc++.h>
const int N = 1e3 + 10;
int dp[N][N];
int main() {
	std::string a, b;
	std::cin >> a >> b;
	int n = a.length(), m = b.length(), max = 0, x = 0;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++) {
			if(a[i] == b[j]) dp[i][j] = dp[i - 1][j - 1] + 1, dp[i][j] > max ? x = i, max = dp[i][j] : 0;
			else dp[i][j] = 0;
		}
	}
	for(int i = x - max + 1; i <= x; i++) std::cout << a[i];
//	std::cout << max << std::endl;
}

5. 判断两个字符串是否匹配

#include<bits/stdc++.h>
using namespace std;
int main(){
	string s, p;
	getline(cin, p);
	getline(cin, s);
	int m = s.length();
    int n = p.length();
    vector<vector<bool> > dp(m + 1, vector<bool>(n + 1, false));
    dp[0][0] = 1;
    for(int i = 1; i <= n; ++i) {
        if(p[i - 1] == '*') dp[0][i] = 1;
        else break;
    }
    for(int i = 1; i <= m; ++i) {
        for(int j = 1; j <= n; ++j) {
            if(s[i - 1] == p[j - 1] || p[j - 1] == '?') {
                dp[i][j] = dp[i - 1][j - 1];
            }else if(p[j - 1] == '*' && (dp[i - 1][j] || dp[i][j - 1])) {
                dp[i][j] = 1;
            }else{
                dp[i][j] = 0;
            }
        }
    }
    if(dp[m][n]) cout << "yes" << endl;
    else cout << "no" << endl;
}

6.最长回文子串 

#include<bits/stdc++.h>
using namespace std;
const int N = 1e3+10;
bool dp[N][N];
int main(){
	string a;
	int n,ans = 1;
	cin >> a;
	a = " " + a;
	n = a.length() - 1;
	int s = 0;
	for(int i = 1;i <= n;i++){
		dp[i][i] = 1;
		if(i < n && a[i] == a[i+1]){
			dp[i][i+1] = 1;
			ans = 2;
			if(!s) s = i;
		}
	}
	for(int i = 3;i <= n; i++){
		bool flag = true;
		for(int l = 1;l + i - 1 <= n; l++){
			int r = l + i - 1;
			if(a[l] == a[r] && dp[l+1][r-1] == 1){
				dp[l][r] = 1;
				ans = i;
				if(flag) s = l, flag = false;
			}
		}
	}
//	cout << ans << endl;
	for(int i = s; i < s + ans; i++) {
		cout << a[i];
	}
}

 7. 年号字串

#include<bits/stdc++.h>
using namespace std;
int main() {
	int n;
	cin >> n;
	int q = 26, l = 1;
	while(q < n) {
		n -= q;
		q *= 26;
		l ++;
	}
	for(int i = 0; i < l; i++) {
		q /= 26;
		for(int j = 0; j < 26; j++) {
			if(n <= q) {
				printf("%c", char('A' + j));
				break;
			} else n -= q;
		}
	}
} 

8.公共钥匙盒——队列 

#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
bool vis[N];
struct cmp{
	bool operator()(const pair<int, pair<int, bool> >& a,pair<int, pair<int, bool> > & b){
		if(a.first > b.first || (a.first == b.first && a.second.second > b.second.second) || (a.first == b.first && a.second.second == b.second.second && a.second.first > b.second.first)) return 1;
		else return 0;
		
	}
};
int main(){
	priority_queue<pair<int, pair<int, bool> >, vector<pair<int, pair<int, bool> > >, cmp> q;
	int n, k;
	cin >> n >> k;
	vector<int> a;
	for(int i = 0; i <= n; i++){
		a.push_back(i);
	}
	while(k-- ){
		int d, s, t;
		cin >> d >> s >> t;
		q.push(make_pair(s, make_pair(d, 1)));
		q.push(make_pair(s + t, make_pair(d, 0)));
	}
	while(!q.empty()){
		pair<int, pair<int, bool> > tmp = q.top();
		q.pop();
//		cout << tmp.first << " " << tmp.second.first << " " << tmp.second.second << endl;
		if(tmp.second.second) {
			for(int i = 1; i <= n; i++)
				if(a[i] == tmp.second.first){
					a[i] = 0;
					break;
				}
		}else {
			for(int i = 1;i <= n; i++)
				if(!a[i]){
					a[i] = tmp.second.first;
					break;
				}
		}
	}
	for(int i = 1; i <= n; i++){
		cout << a[i] << " ";
	}
}

 

举报

相关推荐

0 条评论