如何解决字符串和键的这个问题? [英] How to solve this problem of strings and keys?

查看:92
本文介绍了如何解决字符串和键的这个问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

An Ethical Hacker has been tasked to find vulnerabilities in Transaction Operations of an
Organization XYZ. He has been able to find a relation between the Transaction ID and the Key generated
by the System. Write a program that generates Key from the Transaction ID.
Transaction ID S is a String consisting of only lower case alphabetic characters, i.e. S[i] ∈ [a, z]. K
is said to be a possible key of S, if there exist at least one-character C ∈ [a, z] in each and every substring
of S of length K. Key of ID is the minimum of such possible Ks.
Input
First line contains Transaction ID S (1≤ length(S)≤ 100000)
Output
Key of Transaction ID i.e. Minimum of possible keys Ks
Like if inputs are:-
zbcde   /*here c is common between substrings 
        Having 3 max length i.e zbc cde
bbbbb    /* in this only b is there so length 
         Becomes 1 i.e b
zbzczbz  /*here z is common in maximum length 
         2 of given string i.e zb bz zc cz zb 
          bz 
Then output will be like:-
3
1
2





我尝试过:





What I have tried:

<pre>import java.util.Scanner;

public class lab3hw {
	public static boolean aresame(int[] array)
	{
	    boolean fe = array[0] == 0;
	    for(int i = 1; i < array.length; i++)
	    {
	        if(fe)
	            if(array[i] != 0) return false;
	        else 
	            if(!(array[0]==array[i])) return false;
	    }

	    return true;
	}
	public static boolean samechar(String s)
	{
	    int n = s.length();
	    boolean boo=false;
	    for (int i = 1; i < n; i++)
	        if (s.substring(i,i+1).equals(s.substring(0,1))) {
	            boo=false;
	            
	        }else {
	        	boo=true;
	        }
	    return boo;
	}
	public static int conditions(String str) {  
	 int ci, i, j, k, l=0;
	 int result=0;
     char c, ch;
     i=str.length();
     if(samechar(str)==false) {
    	 
    	 result=1;
     }else {
	     for(c='A'; c<='z'; c++)
	     {
	         k=0;
	         for(j=0; j<i; j++)
	         {
	             ch = str.charAt(j);
	             if(ch == c)
	             {
	                 k++;
	             }
	         }
	         if(k>0)
	         {
	             int[]n=new int[50];
	             for(int m=0;m<i;m++){
	             n[m]=k;
	             }
	         if(aresame(n)==true) { 
	        	 if((str.length())%2==0) {
	        		 result= str.length()/2;}
	        	 else {
	        		 result=(str.length()+1)/2;
	        	 }
	         }else if(aresame(n)==false) {
	        	 
	         }
	         }
	     }
	     
     }
     return result;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner a=new Scanner(System.in);
		String b=a.next();
		System.out.println(conditions(b));
		
	}

}



请告诉我怎么办


Please tell me how can i do

推荐答案

这是你的作业,所以你不会得到任何代码!

但是......这并不复杂,特别是。

开始于找出输入中每个可能值的频率:有很多方法可以做到这一点,但对于这样一个简单的输入,我可能会使用一个数组来计算它们:第一个元素为'a',第二个元素为' b',等等。解析你的字符串,并计算每个字符。当你知道这一点时,你可以找到最高频率:所有'1'表示它是中间字符,只有一个值是明显的,其他意味着你需要处理该行。您可以使用CharAt访问单个角色。



从那开始,看看你能走多远。
This is your homework, so you'll get no code!
But ... this isn't complicated, particularly.
Start by finding out the frequency of each possible value in the input: there are a number of ways to do that, but for such a simple input, I'd probably use an array to count them: first element for 'a', second for 'b', and so forth. Parse your string, and count each character. When you know that, you can find out the highest frequency: all '1's means it's the middle character, only one value is obvious, others means you need to process the line. You can use CharAt to access individual characters.

Start with that, and see how far you get.


这篇关于如何解决字符串和键的这个问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆