CF 두 문제.
17968 단어 c
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
A and B are preparing themselves for programming contests.
After several years of doing sports programming and solving many problems that require calculating all sorts of abstract objects, A and B also developed rather peculiar tastes.
A likes lowercase letters of the Latin alphabet. He has assigned to each letter a number that shows how much he likes that letter (he has assigned negative numbers to the letters he dislikes).
B likes substrings. He especially likes the ones that start and end with the same letter (their length must exceed one).
Also, A and B have a string s. Now they are trying to find out how many substrings t of a string s are interesting to B (that is, t starts and ends with the same letter and its length is larger than one), and also the sum of values of all letters (assigned by A), except for the first and the last one is equal to zero.
Naturally, A and B have quickly found the number of substrings t that are interesting to them. Can you do it?
Input
The first line contains 26 integers xa, xb, ..., xz ( - 105 ≤ xi ≤ 105) — the value assigned to letters a, b, c, ..., z respectively.
The second line contains string s of length between 1 and 105 characters, consisting of Lating lowercase letters— the string for which you need to calculate the answer.
Output
Print the answer to the problem.
Sample test(s)
input
1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1
xabcab
output
2
input
1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1
aaa
output
2
Note
In the first sample test strings satisfying the condition above are abca and bcab.
In the second sample test strings satisfying the condition above are two occurences of aa.
제목: 주어진 문자열 에서 모든 알파벳 은 다음 과 같은 조건 을 만족 시 키 기 위해 고정된 가중치 가 있 습 니 다.
1. 이 문자열 의 이니셜 과 이니셜 이 같다.
2 이 문자열 은 이니셜 을 제외 하고 다른 자모의 가중치 와 0 이다.
사고방식: 접두사 하 나 를 유지 하고 26 개의 hash 로 각각 26 개의 자모의 접두사 와 'a' 를 기 록 했 습 니 다. 만약 에 'a' 를 만 났 다 면 그 와 같은 접두사 가 몇 개 있 는 지 확인 하고 그 위 에 붙 이 고 접두사 와 유지 합 니 다.
1 #include<iostream>
2 #include<cstdio>
3 #include<cstdlib>
4 #include<cstring>
5 #include<string>
6 #include<queue>
7 #include<algorithm>
8 #include<map>
9 #include<iomanip>
10 #include<climits>
11 #include<string.h>
12 #include<cmath>
13 #include<stdlib.h>
14 #include<vector>
15 #include<stack>
16 #include<set>
17 using namespace std;
18 #define INF 1000000007
19 #define MAXN 40010
20 #define Mod 1000007
21 #define N 10007
22 #define NN 30
23 #define sigma_size 3
24 const int maxn = 6e5 + 10;
25 using namespace std;
26 typedef long long LL;
27
28 int h[30];
29 map<LL, LL> m[30];
30 string s;
31 LL cnt, k;
32
33 int main()
34 {
35 for (int i = 0; i < 26; ++i)
36 cin >> h[i];
37 cin >> s;
38 for (int i = 0; i < s.length(); ++i) {
39 cnt += m[s[i] - 'a'][k];
40 k += h[s[i] - 'a'];
41 m[s[i] - 'a'][k]++;
42 }
43 cout << cnt << endl;
44 //system("pause");
45 return 0;
46 }
C. Anya and Smartphone
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Anya has bought a new smartphone that uses Berdroid operating system. The smartphone menu has exactly n applications, each application has its own icon. The icons are located on different screens, one screen contains k icons. The icons from the first to the k-th one are located on the first screen, from the (k + 1)-th to the 2k-th ones are on the second screen and so on (the last screen may be partially empty).
Initially the smartphone menu is showing the screen number 1. To launch the application with the icon located on the screen t, Anya needs to make the following gestures: first she scrolls to the required screen number t, by making t - 1 gestures (if the icon is on the screen t), and then make another gesture — press the icon of the required application exactly once to launch it.
After the application is launched, the menu returns to the first screen. That is, to launch the next application you need to scroll through the menu again starting from the screen number 1.
All applications are numbered from 1 to n. We know a certain order in which the icons of the applications are located in the menu at the beginning, but it changes as long as you use the operating system. Berdroid is intelligent system, so it changes the order of the icons by moving the more frequently used icons to the beginning of the list. Formally, right after an application is launched, Berdroid swaps the application icon and the icon of a preceding application (that is, the icon of an application on the position that is smaller by one in the order of menu). The preceding icon may possibly be located on the adjacent screen. The only exception is when the icon of the launched application already occupies the first place, in this case the icon arrangement doesn't change.
Anya has planned the order in which she will launch applications. How many gestures should Anya make to launch the applications in the planned order?
Note that one application may be launched multiple times.
Input
The first line of the input contains three numbers n, m, k (1 ≤ n, m, k ≤ 105) — the number of applications that Anya has on her smartphone, the number of applications that will be launched and the number of icons that are located on the same screen.
The next line contains n integers, permutation a1, a2, ..., an — the initial order of icons from left to right in the menu (from the first to the last one), ai — is the id of the application, whose icon goes i-th in the menu. Each integer from 1 to n occurs exactly once among ai.
The third line contains m integers b1, b2, ..., bm(1 ≤ bi ≤ n) — the ids of the launched applications in the planned order. One application may be launched multiple times.
Output
Print a single number — the number of gestures that Anya needs to make to launch all the applications in the desired order.
Sample test(s)
input
8 3 3
1 2 3 4 5 6 7 8
7 8 1
output
7
input
5 4 2
3 1 5 2 4
4 4 4 4
output
8
Note
In the first test the initial configuration looks like (123)(456)(78), that is, the first screen contains icons of applications 1, 2, 3, the second screen contains icons 4, 5, 6, the third screen contains icons 7, 8.
After application 7 is launched, we get the new arrangement of the icons — (123)(457)(68). To launch it Anya makes 3 gestures.
After application 8 is launched, we get configuration (123)(457)(86). To launch it Anya makes 3 gestures.
After application 1 is launched, the arrangement of icons in the menu doesn't change. To launch it Anya makes 1 gesture.
In total, Anya makes 7 gestures.
제목: n 개 수 는 몇 개의 칸 에 분포 되 어 있 습 니 다. 한 칸 은 최대 k 개 수 를 담 을 수 있 습 니 다. 담 지 않 아 도 됩 니 다. 매번 한 개의 숫자 에 도착 할 때마다 이 숫자 를 앞의 숫자 와 바 꾸 십시오. (앞 에 다른 숫자 가 없 는 경 우 를 제외 하고) 횟수 를 구 할 수 있 습 니 다.
사고방식: 숫자 와 아래 의 음미 hash 에 표를 저장 하고 매번 한 숫자 에 도달 할 때마다 앞의 숫자 와 다음 표를 교환 합 니 다.
1 #include<iostream>
2 #include<cstdio>
3 #include<cstdlib>
4 #include<cstring>
5 #include<string>
6 #include<queue>
7 #include<algorithm>
8 #include<map>
9 #include<iomanip>
10 #include<climits>
11 #include<string.h>
12 #include<cmath>
13 #include<stdlib.h>
14 #include<vector>
15 #include<stack>
16 using namespace std;
17 #define INF 1000000007
18 #define MAXN 40010
19 #define Mod 1000007
20 #define N 10007
21 #define NN 30
22 #define sigma_size 3
23 const int maxn = 6e5 + 10;
24 using namespace std;
25 typedef long long LL;
26
27 int n, m, k;
28 int x;
29 int a[100100];
30 int b[100100];
31
32 int main()
33 {
34 cin >> n >> m >> k;
35 for (int i = 1; i <= n; ++i) {
36 cin >> a[i];
37 b[a[i]] = i;
38 }
39 LL re = 0;
40 for (int i = 0; i < m; ++i) {
41 cin >> x;
42 re += (b[x]-1) / k + 1;
43 //cout << re << endl;
44 x = b[x];
45 if (x>1)
46 {
47 swap(a[x - 1], a[x]);
48 b[a[x - 1]] = x - 1;
49 b[a[x]] = x;
50 }
51 }
52 cout << re << endl;
53 //system("pause");
54 return 0;
55 }
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Docker를 사용한 React 및 .NET Core 6.0 샘플 프로젝트 - 1부이 기사에서는 Entity Framework Core Code First 접근 방식을 사용하는 ASP.NET Core 6.0 WEP API의 CRUD(만들기, 읽기, 업데이트 및 삭제) 작업에 대해 설명합니다. 웹 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.