Pat 1040

题意是找最长对称子序列,我的算法先将子序列翻转,然后找最长公共连续子序列,同样是用动态规划,和LCS不同的是状态函数
这里写图片描述

#include<iostream>
#include<string.h>

using std::cin;
using std::cout;
using std::endl;

char str[1010],rstr[1010];
int m[1010][1010];
int max(int a, int b){
    return a > b ? a : b;
}
int LongestSym(char str[],char rstr[]){
    int length = strlen(str);
    int maxlen = 0, i, j, flag = 0,len = 0;
    for (i = 0; i < length; i++)
    {
        m[0][i] = 0;
        m[i][0] = 0;
    }
    for (i = 1; i <= length; i++)
        for (j = 1; j <= length; j++)
        {
            if (str[i - 1] == rstr[j - 1])
                m[i][j] = m[i - 1][j - 1] + 1;
            else m[i][j] = 0;
            if (m[i][j]>maxlen)
                maxlen = m[i][j];
        }
        return maxlen;
}
void reverse(char str[]){
    int len = strlen(str);
    int i, j;
    for (i = 0, j = len - 1; j >= 0; j--, i++)
        rstr[i] = str[j];
    rstr[i] = '\0';
}
int main(){
    freopen("1.in", "r", stdin);
    cin.get(str,1009);
    reverse(str);
    int longest = LongestSym(str,rstr);
    cout << longest << endl;
    return 0;
}
### 关于 PAT 乙级真题解析 以下是针对您提到的几道 PAT 乙级真题的具体解答和思路: --- #### 题目 1040:统计字符串中的 PAT 数量 此题的核心在于找到所有可能组成单词 "PAT" 的字符组合。通过遍历输入字符串,记录 P 和 A 出现的位置,并计算 T 后面有多少种合法的 PA 组合。 ```cpp #include <iostream> #include <vector> using namespace std; int main() { string s; cin >> s; long long count_P = 0, count_PA = 0, result = 0; // 使用 long long 避免溢出 for (char c : s) { if (c == 'P') { count_P++; } else if (c == 'A') { count_PA += count_P; } else if (c == 'T') { result += count_PA; } } cout << result % 1000000007; // 取模防止过大 return 0; } ``` 上述代码实现了动态规划的思想[^1],逐步累积符合条件的子序列数量。 --- #### 题目 1026:程序运行时间 该问题主要涉及浮点数处理以及四舍五入操作。可以通过简单的数学运算实现精确的结果转换。 ```cpp #include <cstdio> int main(){ double timeInSeconds; scanf("%lf", &timeInSeconds); printf("%.0f\n", (timeInSeconds / 3600.0 + 0.5)); // 加上偏移量完成四舍五入 return 0; } ``` 这里采用了 `(double + 0.5)` 技巧来简化逻辑[^2],从而达到标准输出的要求。 --- #### 题目 1032:挖掘机组队比赛成绩汇总 这是一道典型的分数累加与比较题目。需要读取多组数据并维护每所学校的成绩表,最后找出最高分对应的学校编号。 ```cpp #include <bits/stdc++.h> using namespace std; struct SchoolScore{ int id; int score; }; bool cmp(const SchoolScore& a,const SchoolScore& b){ return a.score > b.score; } int main(){ vector<SchoolScore> schools(101,{0,0}); // 假设最多有100所不同学校参与竞赛 int n,m,tempId,tempScore; cin>>n>>m; while(m--){ cin>>tempScore>>tempId; schools[tempId].id=tempId; schools[tempId].score+=tempScore; } sort(schools.begin(),schools.end(),cmp); cout<<schools[0].id<<" "<<schools[0].score; return 0; } ``` 这段代码利用结构体存储信息,并借助 STL 排序功能快速定位最优解[^3]。 --- #### 题目 1029:检测损坏按键 本题要求识别哪些字母无法正常打印出来。可以采用集合去重的方式收集异常字符集。 ```cpp #include<stdio.h> #define MAX_LEN 1000 void findBrokenKeys(char* typed,char* expected){ char brokenSet[MAX_LEN]={}; bool isUpper=false; for(int i=0;i<strlen(typed)||i<strlen(expected);i++){ if(i>=strlen(typed)){ addIfNotExists(brokenSet,toupper(expected[i])); } else if(i>=strlen(expected)){ addIfNotExists(brokenSet,toupper(typed[i])); } else if(typed[i]!=expected[i]){ addIfNotExists(brokenSet,toupper(typed[i])); addIfNotExists(brokenSet,toupper(expected[i])); } } printUniqueChars(brokenSet); } // 辅助函数定义省略... ``` 以上片段展示了如何逐字对比两串文本差异之处[^4],并通过辅助方法整理最终答案。 --- ### 总结 这些示例覆盖了多种基础算法和技术要点,包括但不限于字符串匹配、数值变换、数组管理及错误排查等领域知识点。希望它们能帮助理解相关概念并提升实战能力!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值