課后作業
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號