360面试题:偶串(Web)

时间限制:C/C++语言2000MS;其他语言4000MS

内存限制:C/C++语言65536KB;其他语言589824KB

题目描述:

  一个字符串S是偶串,当且仅当S中的没有个字符都出现了偶数次。如字符串“aabccb”是一个偶串,因为字符a,b,c都出现了两次。而字符串“abbcc”不是偶串,因为字符a出现了一次。

  现在给出一个长度为n的字符串T=t1,t2,t3,t4,...,tn。字符串的子串为其中任意连续一段。T长度为1的子串有n个,长度为2的子串为n-1个,以此类推,T一共有n(n-1)/2个子串。给定T,你能算出它有多少个子串是偶串吗?

输入:

  输入一个字符串T,T中只有小写字母。T的长度不超过100000。

输出:

  子串中偶串的个数。

 1 package com.zdt.com;
 2 
 3 import java.util.Scanner;
 4 
 5 public class main {
 6 
 7     public static void main(String[] args) {
 8         // TODO Auto-generated method stub
 9         Scanner scan = new Scanner(System.in);
10         String T =scan.next();
11         int count=0;
12         for(int i = 2;i<=T.length();i+=2){
13             for(int j=0;j<T.length()-i+1;j++){
14                 String test = T.substring(j, j+i);
15                 if(panding(test)){
16                     count++;
17                 }
18             }
19         }
20         System.out.println(count);
21     }
22     public static boolean panding(String test){
23         int a[]= new int[128];
24         //初始化
25         for(int i=0; i<128;i++){
26             a[i]=0;
27         }
28         //进行判定,填充判定数组
29         for(int i=0;i<test.length();i++){
30             int  num  = Integer.valueOf(test.charAt(i));
31             if(a[num]==0){
32                 a[num]+=1;
33             }
34             else{
35                 a[num]-=1;
36             }
37         }
38         //数组求和
39         int sum=0;
40         for(int i=0;i<128;i++){
41             sum+=a[i];
42         }
43         //判定
44         if(sum==0)
45             return true;
46         else
47             return false;
48     }
49 }
Java Code
原文地址:https://www.cnblogs.com/zdtiio/p/6618505.html