- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在解决一个问题来查找此类索引的数量,a, b, c, d
在字符串中 s
, 大小 n
仅由小写字母组成,例如:
1 <= a < b < c < d <= n
s[a] == s[c] and s[b] == s[d]
#include<stdio.h>
int main()
{
int n, count = 0;
char s[2002];
scanf("%d%s", &n, s);
for(int a = 0; a<n-3; a++)
{
for(int b = a + 1; b<n-2; b++)
{
for(int c = b + 1; c<n-1; c++)
{
for(int d = c + 1; d<n; d++)
{
if(s[a] == s[c] && s[b] == s[d] && a>=0 && b>a && c>b && d>c && d<n)
{
count++;
}
}
}
}
}
printf("%d", count);
return 0;
}
a、b、c 和 d 是索引。
The problem statement is available here: https://www.hackerearth.com/practice/algorithms/searching/linear-search/practice-problems/algorithm/holiday-season-ab957deb/
最佳答案
如果您维护一个数组,该数组存储输入字符串中每个字符的累积频率(频率和到目前为止频率分布中的所有频率的总和),则可以解决该问题。由于字符串仅由小写字符组成,因此数组大小将为 [26][N+1]
.
例如:
index - 1 2 3 4 5
string - a b a b a
cumulativeFrequency array:
0 1 2 3 4 5
a 0 1 1 2 2 3
b 0 0 1 1 2 2
我通过将输入字符串的第一个字符的索引作为1来制作数组。这样做有助于我们以后解决问题。现在,只需忽略第 0 列并假设字符串从索引 1 而非 0 开始。
if cumulativeFrequency[i]-cumulativeFrequency[i-1] > 0
字符在 i 到 j 范围内出现的次数(不包括 i 和 j):
frequency between i and j = cumulativeFrequency[j-1] - cumulativeFrequency[i]
1: for each character from a-z:
2: Locate index a and c such that charAt[a] == charAt[c]
3: for each pair (a, c):
4: for character from a-z:
5: b = frequency of character between a and c
6: d = frequency of character after c
7: count += b*d
O(26*n^2*26)
=
O(n^2)
.
//Input N and string
//Do not pay attention to the next two lines since they are basically taking
//input using Java input streams
int N = Integer.parseInt(bufferedReader.readLine().trim());
String str = bufferedReader.readLine().trim();
//Construct an array to store cumulative frequency of each character in the string
int[][] cumulativeFrequency = new int[26][N+1];
//Fill the cumulative frequency array
for (int i = 0;i < str.length();i++)
{
//character an index i
char ch = str.charAt(i);
//Fill the cumulative frequency array for each character
for (int j = 0;j < 26;j++)
{
cumulativeFrequency[j][i+1] += cumulativeFrequency[j][i];
if (ch-97 == j) cumulativeFrequency[j][i+1]++;
}
}
int a, b, c, d;
long count = 0;
//Follow the steps of the algorithm here
for (int i = 0;i < 26;i++)
{
for (int j = 1; j <= N - 2; j++)
{
//Check if character at i is present at index j
a = cumulativeFrequency[i][j] - cumulativeFrequency[i][j - 1];
if (a > 0)
{
//Check if character at i is present at index k
for (int k = j + 2; k <= N; k++)
{
c = cumulativeFrequency[i][k] - cumulativeFrequency[i][k - 1];
if (c > 0)
{
//For each character, find b*d
for (int l = 0; l < 26; l++)
{
//For each character calculate b and d
b = cumulativeFrequency[l][k-1] - cumulativeFrequency[l][j];
d = cumulativeFrequency[l][N] - cumulativeFrequency[l][k];
count += b * d;
}
}
}
}
}
}
System.out.println(count);
关于c - 如何降低遍历字符串的时间复杂度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66840351/
我在堆栈上创建了这段代码: function increase_brightness(hex, percent){ var r = parseInt(hex.substr(1, 2), 16)
为什么我能够LOWER COALESCE 中的每个项目,但无法LOWER整个COALESCE,否则我会遇到语法错误?例如: SELECT COALESCE(LOWER(google_provider_
我在谷歌上搜索到的所有内容都表明,以下任何一项都会将 double 舍入到小数点后两位。 double roundToFourDecimals(double d) { DecimalForma
我正在开发一个 flexdashboard/storyboard,我想在其中降低每个帧的高度。那可能吗? 示例代码: --- title: "Flex" output: flexdashboard
我在 WPF 中有一个图像控件。我需要减小图像尺寸控件的宽度和高度。但是当我这样做时,图像看起来不太好。数据丢失更多。 所以我想降低图像分辨率而不是仅仅改变图像控件的宽度和高度。 任何人都可以帮助我如
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题?通过 editing this post 添加详细信息并澄清问题. 1年前关闭。 Improve this
我正在扩展 Fluent NHibernate,以便更好地与 F# 一起使用(即引用支持),并希望获得一些关于降低 API 流畅性的反馈。 F# 要求使用返回值,除非它们是单位类型。所以这最终以“|>
我们有一个 BizTalk 2010 接收位置,它将获取一个 70MB 的文件,然后使用入站映射(在接收位置)和出站映射(在发送端口)生成一个 1GB 文件。 执行上述过程时,SQL Server 会
我的代码分析插件提示包含以下代码的方法中的代码复杂性。我注意到以下代码看起来可以组合,但我不知道如何做到这一点: for(Command command : commands) { if (c
我正在寻找一种方法来始终忽略 R 中 float 之间的微小差异(根据 IEC 60559,这些是 double 浮点),通过使用基本 R 工具而不诉诸 C 或 C++。换句话说,我想“四舍五入” d
在 Blazor 中使用 ChartJs.Blazor 的 BarChart 组件时是否可以降低甚至关闭动画速度?我发现这个 NuGet 包非常有用,但我不知道如何在更新条形图时关闭动画。为了更容易忽
所以我为一个游戏编写了这段代码,现在该游戏的速度非常快。我想降低 FPS,让游戏慢一点。 我认为我唯一的出路就是制作一个计时器。但我发现很难找到放置计时器的位置?谁能帮我解决这个问题吗? 所以我为一个
我正在编写一个程序,我担心它运行所需的时间和所占用的空间。 在程序中我使用了一个变量来存储数组的长度: int len=newarray3.length; 现在,我想知道是否能够通过不使用 len 变
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 2 年前。 Improve th
我用Java编写了一个程序,但它的计算时间很长,我不知道为什么。有人可以指点一下以降低复杂性吗?此外,在计算一些值(例如 3,100 之后)后,它会给出空指针异常。代码: public class F
我有下图,由 1 行 2 列的网格组成。我愿意 降低右侧子图的高度(3D PREDICTION),使棋盘平面看起来有点挤压并显示更好的视角。 在左侧子图的顶部添加一些边距(2D PREDICTION)
是否有一种简单的方法可以更改以 RGB 字符串形式给出的颜色的亮度? 例如 in_RGB = '#FF0000' --> out_RGB = '#CC0000' 最佳答案 将十六进制字符串转换为 R
我已经编写了代码来更改对象(不是进程)(在本例中是文件)的完整性级别。据我们所知,我们从中等完整性级别开始,但我想将其降低到“低”。我想运行完整性较低的 .txt 文件而不是默认介质。 我使用 WIN
是否可以在保持原始宽高不变的情况下降低图像分辨率? 我已经使用 BitmapFactoryOptions 尝试了几个选项: 在样本大小 inDensity、inScaled、inTargetDensi
是否有高级(Java)或低级方式(使用 native 代码)将 Android 设备上的蓝牙信号强度更改为最低? 目标是使设备在 20 厘米范围内可被发现?在 Internet 上根本找不到与此相关的
我是一名优秀的程序员,十分优秀!