, n(1<=n<=100000), n , 。 1-9 x, x ; X ; # 。 。
|
|
|
|
C/C++ :2000MS
:4000MS
|
C/C++ :65536KB
:589824KB
|
:
package cn.com.jingdong;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
String str = sc.nextLine();
// T( list) , , X , T 。 :Ti=j j , i 。
List list = new ArrayList();
// P , P , 0, , ,
// :Pjk= 1.....Pj+k= 1 jk j+k 。j ,k 。
int[] p = new int[n];
for (int i = 0; i < n; i++) {
char temp = str.charAt(i);
if (temp == 'X') {
list.add(i);
} else if ('1' <= temp && temp <= '9') {
int left = Math.max(0, i - (temp - '0'));
int right = Math.min(i + (temp - '0'), n - 1);
for (int x = left; x <= right; x++) {
p[x] = 1;
}
}
}
int count = 0;
// :PTi= 1 i 。 :count=count+ 1
for (int i = 0; i < list.size(); i++) {
if (p[list.get(i)] == 1) {
count++;
}
}
System.out.println(count);
}
}
:
package cn.com.jingdong;
import java.util.Scanner;
public class Main3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
String str = sc.nextLine();
char[] a = str.toCharArray();
int count = 0;
for (int i = 0; i < n; i++) {
char temp = a[i];
if ('1' <= temp && temp <= '9') {
//left,right
int left = Math.max(0, i - (temp - '0'));
int right = Math.min(i + (temp - '0'), n - 1);
for (int x = left; x <= right; x++) {
if(a[x]=='X'){// , +1, ,
count++;
a[x]='#';
}
}
}
}
System.out.println(count);
System.out.println(str);
}
}
:
package cn.com.jingdong;
import java.util.HashSet;
import java.util.Scanner;
public class Main3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String s = sc.next();
HashSet set = new HashSet<>();// , set
for (int i = 0; i < n; i++) {
if (Character.isDigit(s.charAt(i))) {
int k = s.charAt(i) - '0';
if (k == 0)
continue;
int l = Math.max(0, i - k);
int r = Math.min(n - 1, i + k);
for (int x = l; x <= r; x++) {
if (s.charAt(x) == 'X')
set.add(x);
}
}
}
System.out.println(set.size());
}
}