P2957

文章描述了一个编程问题,涉及寻找两个奶牛哞叫及其回音字符串之间的最长公共重复部分,即同时是前缀和后缀的子串。给出两个示例字符串,要求编写程序找出最长重叠长度。

题目描述

The cows enjoy mooing at the barn because their moos echo back, although sometimes not completely. Bessie, ever the excellent

secretary, has been recording the exact wording of the moo as it goes out and returns. She is curious as to just how much overlap there is.

Given two lines of input (letters from the set a..z, total length in the range 1..80), each of which has the wording of a moo on it, determine the greatest number of characters of overlap between one string and the other. A string is an overlap between two other strings if it is a prefix of one string and a suffix of the other string.

By way of example, consider two moos:

moyooyoxyzooo
yzoooqyasdfljkamo

The last part of the first string overlaps 'yzooo' with the first part of the second string. The last part of the second string

overlaps 'mo' with the first part of the first string. The largest overlap is 'yzooo' whose length is 5.

POINTS: 50

奶牛们非常享受在牛栏中哞叫,因为她们可以听到她们哞声的回音。虽然有时候并不能完全听到完整的回音。Bessie 曾经是一个出色的秘书,所以她精确地纪录了所有的哞叫声及其回声。她很好奇到底两个声音的重复部份有多长。

输入两个字符串(长度为 1 到 80 个字母),表示两个哞叫声。你要确定最长的重复部份的长度。两个字符串的重复部份指的是同时是一个字符串的前缀和另一个字符串的后缀的字符串。

我们通过一个例子来理解题目。考虑下面的两个哞声:

moyooyoxyzooo
yzoooqyasdfljkamo

第一个串的最后的部份 yzooo 跟第二个串的第一部份重复。第二个串的最后的部份 mo 跟第一个串的第一部份重复。所以 yzooo 跟 mo 都是这 22 个串的重复部份。其中,yzooo 比较长,所以最长的重复部份的长度就是 55。

输入格式

* Lines 1..2: Each line has the text of a moo or its echo

输出格式

* Line 1: A single line with a single integer that is the length of the longest overlap between the front of one string and end of the other.

输入输出样例

输入 #1复制

abcxxxxabcxabcd 
abcdxabcxxxxabcx 

输出 #1复制

11 

说明/提示

'abcxxxxabcx' is a prefix of the first string and a suffix of the second string.

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int ans=-1;
    string a,b;
    cin>>a>>b;
    for(int i=0;i<a.size();i++)
    for(int j=0;j<=a.size()-i;j++)
   		if(b.find(a.substr(i,j))!=-1){
			if(j>ans){
    			ans=j;
			}
   	}
    cout<<ans;
    return 0;
}

<think>我们遇到了一个错误:在代码中使用了未定义的变量 `P_grid`。问题出现在子图4的绘制中: ```matlab contour(x_unique, y_unique, P_grid, 20, 'LineColor', 'k', 'LineWidth', 0.5) ``` 但是,在之前的代码中,我们并没有计算或定义 `P_grid`。根据原始数据,我们只加载了U和V分量,并没有加载压力P。因此,我们需要移除或替换这部分代码。 ### 解决方案: 1. **移除压力相关的绘图**:因为我们没有压力数据,所以不能绘制压力等值线。 2. **替换为其他物理量**:我们可以绘制涡量等值线或速度等值线来代替。 ### 修改步骤: 在子图4中,我们原本想用压力等值线作为背景,但现在没有压力数据。我们可以改为绘制涡量等值线,因为涡量已经计算过。 将子图4中的: ```matlab contour(x_unique, y_unique, P_grid, 20, 'LineColor', 'k', 'LineWidth', 0.5) ``` 修改为: ```matlab contour(x_new, y_new, vorticity, 20, 'LineColor', 'k', 'LineWidth', 0.5) ``` 注意:同时要确保坐标使用的是插值后的网格坐标(`x_new` 和 `y_new`)。 ### 完整修改后的子图4代码: ```matlab % 子图3:Q准则涡识别 subplot(2, 2, 4) % 创建Q准则掩码 vortex_mask = Q > q_threshold * max(Q(:)); % 可视化涡核区域 contourf(x_new, y_new, double(vortex_mask), [0.5, 1.5], 'LineColor', 'none') hold on % 替换压力等值线为涡量等值线 contour(x_new, y_new, vorticity, 20, 'LineColor', 'k', 'LineWidth', 0.5) % 标记涡核中心 [centers_x, centers_y] = find_vortex_centers(x_new, y_new, vortex_mask); plot(centers_x, centers_y, 'ro', 'MarkerSize', 8, 'LineWidth', 1.5) colormap([1 1 1; 0 0.5 1]) % 背景白色,涡核蓝色 title(sprintf('Q准则涡识别 (阈值 = %.2f)', q_threshold)) xlabel('X') ylabel('Y') axis equal tight ``` ### 另外,注意之前的错误处理: 在之前的错误中,我们还发现 `contourf` 函数使用了 `x_unique` 和 `y_unique` 而数据是插值后的网格。因此,我们需要将整个代码中所有的可视化坐标都统一替换为 `x_new` 和 `y_new`。 ### 总结修改点: 1. 替换子图4中的压力等值线为涡量等值线(使用 `vorticity` 变量)。 2. 确保所有绘图的坐标都是插值后的网格坐标 `x_new` 和 `y_new`(而不是原始的 `x_unique` 和 `y_unique`)。 3. 注意辅助函数 `find_vortex_centers` 的输入坐标也应该是 `x_new` 和 `y_new`。 ### 完整修改后的代码片段(针对子图部分): ```matlab %% 6. 可视化涡结构 figure('Position', [100, 100, 1200, 800], 'Color', 'white') % 计算速度大小(替代压力场) speed = sqrt(U.^2 + V.^2); % 若为结构化网格(已插值) speed_grid = reshape(speed, ny, nx); % 注意:这里ny和nx是插值后的尺寸 % 子图1:速度大小和涡量等值线 subplot(2, 2, [1, 3]) contourf(x_new, y_new, speed_grid, 50, 'LineColor', 'none') % 用速度大小 hold on contour(x_new, y_new, vorticity, 20, 'LineColor', [0.3 0.3 0.3]) title('速度大小和涡量线') % 修改标题 colorbar xlabel('X') ylabel('Y') axis equal tight % 子图2:涡量云图 subplot(2, 2, 2) vorticity_magnitude = abs(vorticity); contourf(x_new, y_new, vorticity_magnitude, 50, 'LineColor', 'none') hold on % 绘制速度矢量(稀疏化) skip = 10; % 跳过的点数 % 注意:U_grid 和 V_grid 已经是插值后网格上的数据,大小为 [ny, nx] quiver(x_new(1:skip:end), y_new(1:skip:end),... U_grid(1:skip:end, 1:skip:end), V_grid(1:skip:end, 1:skip:end),... scale_factor, 'k', 'LineWidth', 0.7) colormap(parula) colorbar title('涡量强度') xlabel('X') ylabel('Y') axis equal tight % 子图3:Q准则涡识别 subplot(2, 2, 4) % 创建Q准则掩码 vortex_mask = Q > q_threshold * max(Q(:)); % 可视化涡核区域 contourf(x_new, y_new, double(vortex_mask), [0.5, 1.5], 'LineColor', 'none') hold on % 绘制涡量等值线 contour(x_new, y_new, vorticity, 20, 'LineColor', 'k', 'LineWidth', 0.5) % 标记涡核中心 [centers_x, centers_y] = find_vortex_centers(x_new, y_new, vortex_mask); plot(centers_x, centers_y, 'ro', 'MarkerSize', 8, 'LineWidth', 1.5) colormap([1 1 1; 0 0.5 1]) % 背景白色,涡核蓝色 title(sprintf('Q准则涡识别 (阈值 = %.2f)', q_threshold)) xlabel('X') ylabel('Y') axis equal tight ``` ### 注意: 1. 在计算梯度时,我们使用了 `x_new` 和 `y_new`(它们是插值后的网格坐标向量),所以 `vorticity` 和 `Q` 都是基于插值后的网格计算的,因此它们的大小是 `[ny, nx]`(即7行2957列)。 2. 在绘制等值线时,必须使用相同网格的坐标(`x_new` 和 `y_new`)。 ### 代码调试建议: 在运行代码之前,可以通过以下命令检查变量维度: ```matlab fprintf('x_new size: %d, y_new size: %d\n', length(x_new), length(y_new)); fprintf('vorticity size: [%d, %d]\n', size(vorticity,1), size(vorticity,2)); ``` 确保 `vorticity` 的第一维等于 `y_new` 的长度(7),第二维等于 `x_new` 的长度(2957)。 ### 最终保存图像部分保持不变。 ### 相关问题: §§相关问题§§ 1. 在流场可视化中,除了Q准则,还有哪些涡识别方法? 2. 如何优化非结构化网格的插值精度? 3. 对于高长宽比网格(如2957×7),如何提高涡量计算的准确性? </think>### 解决"函数或变量 'P_grid' 无法识别"错误 您遇到的错误是由于代码中使用了未定义的变量`P_grid`。这个问题出现在子图4的绘制过程中: ```matlab contour(x_unique, y_unique, P_grid, 20, 'LineColor', 'k', 'LineWidth', 0.5) ``` #### 错误原因分析 1. **变量未定义**: - 代码中只有`U`(x方向速度)和`V`(y方向速度)的定义 - 没有加载或计算压力场(`P`)数据 - `P_grid`变量从未被声明或赋值 2. **数据来源问题**: - 原始POD结果文件中只包含速度场数据(U,V) - 代码试图绘制不存在的压力场等值线 #### 解决方案 以下是三种解决方法,推荐使用第一种: **方案1:替换为速度场可视化(推荐)** ```matlab % 修改前(错误代码): % contour(x_unique, y_unique, P_grid, 20, 'LineColor', 'k', 'LineWidth', 0.5) % 修改后: speed_magnitude = sqrt(U.^2 + V.^2); % 计算速度大小 speed_grid = reshape(speed_magnitude, ny, nx); % 重塑为网格 contour(x_new, y_new, speed_grid, 20, 'LineColor', 'k', 'LineWidth', 0.5) ``` **方案2:替换为涡量场可视化** ```matlab % 修改后: contour(x_new, y_new, vorticity, 20, 'LineColor', 'k', 'LineWidth', 0.5) ``` **方案3:完全移除压力场相关代码** ```matlab % 删除以下行: % contour(x_unique, y_unique, P_grid, 20, 'LineColor', 'k', 'LineWidth', 0.5) ``` #### 完整修正后的子图4代码 ```matlab % 子图3:Q准则涡识别 subplot(2, 2, 4) % 创建Q准则掩码 vortex_mask = Q > q_threshold * max(Q(:)); % 可视化涡核区域 contourf(x_new, y_new, double(vortex_mask), [0.5, 1.5], 'LineColor', 'none') hold on % === 修改部分开始 === % 使用速度场替代压力场 speed_magnitude = sqrt(U.^2 + V.^2); speed_grid = reshape(speed_magnitude, ny, nx); contour(x_new, y_new, speed_grid, 20, 'LineColor', 'k', 'LineWidth', 0.5) % === 修改部分结束 === % 标记涡核中心 [centers_x, centers_y] = find_vortex_centers(x_new, y_new, vortex_mask); plot(centers_x, centers_y, 'ro', 'MarkerSize', 8, 'LineWidth', 1.5) colormap([1 1 1; 0 0.5 1]) % 背景白色,涡核蓝色 title(sprintf('Q准则涡识别 (阈值 = %.2f)', q_threshold)) xlabel('X') ylabel('Y') axis equal tight ``` ### 关键修改说明 1. **坐标系统一**:所有可视化使用插值后网格坐标`(x_new, y_new)` 2. **数据兼容性**:使用已计算的速度场数据`(U, V)`替代缺失的压力场 3. **维度匹配**:确保`speed_grid`尺寸为$7 \times 2957$,与网格匹配 ### 流场可视化最佳实践 对于高长宽比网格($2957 \times 7$),建议: 1. **优化插值**:使用`scatteredInterpolant`提高插值精度 ```matlab F = scatteredInterpolant(X, Y, speed_magnitude); speed_grid = F(X_grid, Y_grid); ``` 2. **增强可视化**:使用`pcolor`替代`contourf`提升渲染效率 ```matlab pcolor(x_new, y_new, speed_grid) shading interp ``` 3. **自动阈值**:动态计算Q准则阈值 ```matlab q_threshold = 0.3 * max(Q(:)); % 自适应阈值 ``` ### 补充说明 若确实需要压力场数据,需修改第一串POD计算代码,增加压力场保存: ```matlab % 在POD计算代码中添加: save(pod_result_path, 'Phi_POD', 'Coeff_POD', 'lam_POD', 'xmean', 'X', 'Y', 'num_points', 'num_vars', 'P'); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值