课后作业
class Grandparent
{
public Grandparent()
{
System.out.println("GrandParent Created.");
}
public Grandparent(String string)
{
System.out.println("GrandParent Created.String:" + string);
}
}
class Parent extends Grandparent
{
public Parent()
{
//super("Hello.Grandparent.");
System.out.println("Parent Created");
// super("Hello.Grandparent.");
}
}
class Child extends Parent
{
public Child()
{
System.out.println("Child Created");
}
}
public class TestInherits
{
public static void main(String args[])
{
Child c = new Child();
}
}
代码输出结果为
GrandParent Created.
Parent Created
Child Created
说明在调用构造方法时,会先调用父类的构造方法,再调用子类的。如果去掉第一处注释,输出结果为
GrandParent Created.String:Hello.Grandparent.
Parent Created
Child Created
这里用super显示调用了父类中含参的构造方法,但如果去掉第二处注释,代码会报错,因为super必须是构造方法主体的第一条语句。如果构造方法中没有写super,则会自动调用父类的无参构造方法
链接:https://ac.nowcoder.com/acm/problem/303942
来源:牛客网
小红喜欢
1
1,小紫喜欢
2
2,小彩喜欢
3
3。
她们定义一个字符串是好字符串,当且仅当字符串中同时存在
1
,
2
,
3
1,2,3 且数量相同。
现在有一个仅由
1
,
2
,
3
1,2,3 组成的长为
𝑛
n 的字符串,小彩想知道,这个字符串有多少子串是好字符串?
【名词解释】
子串:从原字符串中,连续的选择一段字符(可以全选、可以不选)得到的新字符串
include <bits/stdc++.h>
define int long long
define endl '\n'
define INF 1000000000000000000
using namespace std;
const int N = 2e6 + 10;
const int mod=1e9+7;
const int PR=998244353;
int n, m;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
int a[N];
void debug(vector
for (int i = l; i <= r; ++i) {
cout << v[i] << ' ';
}
cout << endl;
return ;
}
//ShekongAA is really Shekong
void solve() {
cin >> n;
string s;
cin >> s;
if(n < 3){
cout << 0 << endl;
return ;
}
int ans = 0;
s = " " + s;
map<int, int>mp;
for(int i = 1; i <= n; i++){
mp[1] = mp[2] = mp[3] = 0;
for(int j = i; j <= n; j++){//以i为左边界,j为右边界
mp[s[j] - '0']++;
if(mp[1] == mp[2] && mp[2] == mp[3])ans ++;
}
}
cout << ans;
return ;
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int test = 1;
// cin >> test;
while (test--) {
solve();
}
return 0;
}

浙公网安备 33010602011771号