public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); }
public void solve() throws Exception { String str = in.readLine(); int n = str.length(); int t = Integer.parseInt(in.readLine()); int[][] dp = new int[26][n]; dp[str.charAt(0) - 'a'][0] = 1; for (int i = 1; i < n; i++) { for (int j = 0; j < 26; j++) { if (str.charAt(i) == 'a' + j) { dp[j][i] = dp[j][i - 1] + 1; } else { dp[j][i] = dp[j][i - 1]; } } } while (t-- > 0) { st = new StringTokenizer(in.readLine()); char a = st.nextToken().toCharArray()[0]; char b = st.nextToken().toCharArray()[0]; int l = parseInt(st.nextToken()) - 1; int r = parseInt(st.nextToken()) - 1; int ans = 0; for (int i = l; i <= r; i++) { if (str.charAt(i) == a) { ans += dp[b - 'a'][r] - dp[b - 'a'][i]; } } out.println(ans); } }