Dowemo
0 0 0 0

http://exercise. Acmcoder. com/online/online_judge_ques? ques_id = 3364 & amp; konwledgeid = 40.

Degree bear finding substring ( baidu 2017 real question )

 题目描述

The degree bear receives a string s with only lowercase letters, which is interested in s 's substring, and s 's substring is a continuous segment of s. He found that some substrings were composed of only one letter, and he wanted to know how many strings there were in s.
For example, in string"aaabbaa", degree bear wants to find a substring with"a","aa","aaa","b","bb"five,"bb"five.

( in this case only the number of substrings is considered, the same substring appears only once in multiple locations )

Enter
Enter only one line, one string, length not exceeding 100000, and only lowercase
Sample input
aaabbaa
Output
Output one line and the number of required substring
Sample output
5
Time limit
C/c + + language: 1000 ms other languages: 3000 ms
Memory limit
C/c + + language: 65536 kb other languages: 589824 kb
Select language:
Java8
Compiler environment description

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;/**
 *http://exercise.acmcoder.com/online/online_judge_ques?ques_id=3364&konwledgeId=40
 */publicclass Main {
 publicstaticvoidmain(String[] args) {
 Scanner in = new Scanner(System.in);
 String string = in.nextLine().trim();
 System.out.println(numOfSubString(string));
 }
 privatestaticintnumOfSubString(String string) {
 int index = 0;
 Set<String> set = new HashSet<>();
 for(int i = 0;i<string.length();i++) {
 index = string.charAt(index)==string.charAt(i)?index:i;
 set.add(string.substring(index, i+1));
 }
 returnset.size();
 }
}



Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs