10008 - What's Cryptanalysis?

All about problems in Volume 100. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Jordi Aranda
New poster
Posts: 13
Joined: Wed Apr 29, 2009 11:37 am
Location: Barcelona

Re: 10008 - What's Cryptanalysis?

Post by Jordi Aranda »

I have a concrete question regarding C++ language:

For example, in this problem, we type n before all, and then n lines are followed.
Why if I use getline(cin,s) function, I can only type 2 lines rather than 3?

Code: Select all

for(int j = 0; j < n; ++j){
        string s;
        getline(cin,s);
        ...
I had to put a cin.ignore() before this in order to fix it and got A after it.
Thx in advance for the response(s).
Born to be wild
valkov
New poster
Posts: 20
Joined: Tue Jul 20, 2010 3:11 pm

Re: 10008 - What's Cryptanalysis?

Post by valkov »

Just got AC on this problem.
Some tips:
1. Use something like:

Code: Select all

char c;
while( (c = getchar()) != EOF)
    if(isalpha(c))
      // handle char    
for handling your input. You don't care about individual lines!
2. I think that my implementation is too complex for such problem but is only 45 lines of C++. So consider using maps and "Sorting map by value" ( You should think about that a lil bit, because you actually can't do it :) )
3. Have fun :)
elmagnifico
New poster
Posts: 5
Joined: Mon Apr 30, 2012 10:15 am

Re: 10008 - What's Cryptanalysis?

Post by elmagnifico »

Ok.. I tried test cases as many as I could think of.. But i'm still getting WA.. Can sumone point out the bugs?

Code: Select all

#include<iostream>
#include<cstdlib>
#include<cctype>
#include<cstring>
#include<string>
#include<vector>
using namespace std;
int main()
{
    int i,j,k,c=0,n;string s;cin>>n;vector<int> freq(26,0);
    while(n-- && n>=0 )
    {
       cin.clear();cin.sync();
       getline(cin,s);
       for(i=0;i<s.length();i++)
       s[i]=toupper(s[i]);
       for(i=0;i<s.length();i++)
       freq[(int)s[i]-65]++;
       
       c=c+s.length();   
    }
    for(i=c;i>=1;i--)
       {
          for(j=0;j<26;j++)
          {
            if(freq[j]==i)
            cout<<(char)(65+j)<<" "<<i<<endl;
          }
       }
    system("pause");
    return 0;
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10008 - What's Cryptanalysis?

Post by brianfry713 »

It looks like you figured it out.
Check input and AC output for thousands of problems on uDebug!
Pauli31
New poster
Posts: 1
Joined: Fri Sep 21, 2012 9:57 pm

10008 - What's Cryptanalysis? Runtime error

Post by Pauli31 »

Hi please help me. what is wrong ? when i upload my code then i get Runtime error

Code: Select all

import java.util.*;
import java.io.*;

class Main
{

    public static void main(String args[]){
         Main w = new Main();
         w.Begin();
    }

         void Begin(){
    vypis(bubleSort(pocet_pismen(scan())));
    
    }
    
   
    
     static int[][] bubleSort(int[][] pole){
      
       
        for (int i=0;i<pole.length-1;i++) {
            
            for (int j=0;j<pole.length-i-1;j++) {
                if(pole[j][1] < pole[j+1][1]){
                    int a = pole[j][1];
                    int b = pole[j][0];
                    pole[j][1] = pole[j+1][1];
                    pole[j][0] = pole[j+1][0];
                    pole[j+1][1] = a;
                    pole[j+1][0] = b;
                }
            }
        }   
        return pole;
    }
    
    
    
    static void vypis(int[][] pole){
        
        for(int i =0;i<pole.length;i++){
            if(pole[i][1]==0)
                continue;
            System.out.println((char)pole[i][0]+" "+pole[i][1]);
        }
    
    }
    
 
    //spocita ktere pismeno se v textu kolikrat vyskytuje vrati pole kde prvni index urcuje znak v unicode
    // a druhy index pole pocet kolikrat se opakuje
   static int[][] pocet_pismen(String vstup)
    {
        vstup = zbav_mezer(vstup); // zbaví ret. mezer
        int [][]pole = new int [26][2];
        if(vstup.length()>0){
        
            int misto = 65;
        // naplní pole základními hodnotam
            for(int i = 0;i<26;i++){
                int b = misto;
                pole[i][0]=b;
                pole[i][1]=0;
                misto++;
            }
            // po?ítání samotných písmen
            for(int j=0;j<vstup.length();j++){
                char c = vstup.charAt(j);
                int poz = (int)c;
                 poz-=65;
                 pole[poz][1]++;
        
            }
            
        }else{
            
        }
        return pole;
    }
    
     // zbaví string mezer a nezadoucich znaku a p?evede ret na velká písmena
   static  String zbav_mezer(String a){
    String n="";
    char pom;
    a = a.trim();
    a = a.toUpperCase();
    for(int i =0;a.length()>i;i++)
        {
          pom = a.charAt(i);
          int s = (int)pom;
          
          if((s>=65)&&(s<=90))    {        
            n = n+pom;
          }
        
    }
    
     return n;
}
     // na?te znaky
   static  String scan(){
   
      
            Scanner sc = new Scanner(System.in);
            int radek = sc.nextInt();
            
            String ret="";
            if(radek!=0){
            
                for(int i =0;i<radek;i++){
                 sc = new Scanner(System.in);
                 ret = ret.concat(sc.nextLine());
                    
                
                }
            }
          
            return ret;
    

     }
}
 
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10008 - What's Cryptanalysis? Runtime error

Post by brianfry713 »

Running on the sample input:

Code: Select all

Exception in thread "main" java.util.NoSuchElementException: No line found
	at java.util.Scanner.nextLine(Scanner.java:1516)
	at Main.scan(Main.java:113)
	at Main.Begin(Main.java:13)
	at Main.main(Main.java:9)
Check input and AC output for thousands of problems on uDebug!
Valisek
New poster
Posts: 2
Joined: Thu Oct 02, 2014 12:09 am

10008 - What's Cryptanalysis?

Post by Valisek »

Hello,
I made this program, works in BlueJ, works on ideone.com btu i cant run it on this site. I always get runtime error, tried everything but it doesnt work here. Any suggestions?

Thanks a lot!

Code: Select all

import java.util.Scanner;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

class test
{
	
   private static int pocetVstupu = 0;
   private static String text = "";
   private static Map<Character, Integer> abeceda;
   
	public static void main (String[] args) throws java.lang.Exception
	{
		 abeceda = new HashMap<Character, Integer>();
        for(char c = 'a'; c <= 'z'; c++){
           abeceda.put(c, 0);
        }
        Scanner sc = new Scanner(System.in);
        System.out.print("Zadejte pocet radku: ");
        try{
            pocetVstupu = sc.nextInt();
        }catch(Exception e){
            e.printStackTrace();
        }
        
        
        for(int k = 1; k <= pocetVstupu; k++){
           sc = new Scanner(System.in);
           text += sc.nextLine();
        }
        
        text = text.toLowerCase();
          for(int i = 0; i < text.length(); i++){
              if(text.charAt(i)>='a' && text.charAt(i)<= 'z'){
              abeceda.put(text.charAt(i),abeceda.get(text.charAt(i))+1);
             }
            }
            
        Set<Entry<Character, Integer>> set = abeceda.entrySet();
        List<Entry<Character, Integer>> list = new ArrayList<Entry<Character, Integer>>(set);
        Collections.sort( list, new Comparator<Map.Entry<Character, Integer>>()
        {
            public int compare( Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2 )
            {
                return (o2.getValue()).compareTo( o1.getValue() );
            }
        } );
            for(Map.Entry<Character, Integer> entry:list){
            Integer value = entry.getValue();
            if(value > 0){
              
              System.out.println(entry.getKey() + " " + value);
            }
          //System.out.println(abeceda);
         }
         System.exit(0);
	}
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10008 - What's Cryptanalysis?

Post by brianfry713 »

Next time post in the existing thread.
Use class Main
Check input and AC output for thousands of problems on uDebug!
Valisek
New poster
Posts: 2
Joined: Thu Oct 02, 2014 12:09 am

Re: 10008 - What's Cryptanalysis?

Post by Valisek »

Ok, I'M sorry :)

I used it like this, doesnt work too

Code: Select all

import java.util.Scanner;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.io.*;

class Main
{   
   
   public static void main (String[] args) throws IOException
   {
       Main myWork = new Main();
       myWork.Begin();
   }
   
   void Begin() throws IOException{
    int pocetVstupu = 0;
    String text = "";
    Map<Character, Integer> abeceda;
    Scanner sc;
     abeceda = new HashMap<Character, Integer>();
        for(char c = 'A'; c <= 'Z'; c++){
           abeceda.put(c, 0);
        }
        
      //  BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); 

        //pocetVstupu = Integer.parseInt(in.readLine()); 
        sc = new Scanner(System.in);
        System.out.print("Zadejte pocet radku: ");
        try{
            pocetVstupu = sc.nextInt();
            sc.nextLine();
        }catch(Exception e){
            e.printStackTrace();
        }
        
        
        for(int k = 1; k <= pocetVstupu; k++){
           sc = new Scanner(System.in);
           text += sc.nextLine();
        }
        
        text = text.toUpperCase();
          for(int i = 0; i < text.length(); i++){
              if(text.charAt(i)>='A' && text.charAt(i)<= 'Z'){
              abeceda.put(text.charAt(i),abeceda.get(text.charAt(i))+1);
             }
            }
            
        Set<Entry<Character, Integer>> set = abeceda.entrySet();
        List<Entry<Character, Integer>> list = new ArrayList<Entry<Character, Integer>>(set);
        Collections.sort( list, new Comparator<Map.Entry<Character, Integer>>()
           {
            public int compare( Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2 )
            {
                return (o2.getValue()).compareTo( o1.getValue() );
            }
          } );
            for(Map.Entry<Character, Integer> entry:list){
            Integer value = entry.getValue();
            if(value > 0){
              
              System.out.println(entry.getKey() + " " + value);
            }
            //System.out.println(abeceda);
         }
         
    }
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10008 - What's Cryptanalysis?

Post by brianfry713 »

Try running your code on the sample input:
http://ideone.com/ugxvYN
Check input and AC output for thousands of problems on uDebug!
osongayito
New poster
Posts: 2
Joined: Wed Aug 26, 2015 8:10 am

Re: 10008 - What's Cryptanalysis?

Post by osongayito »

Hello all

I've submitted the problem several times but getting wrong answer.
My current version reads till EOF. Previously I've tried with getting only N lines.
My code works with the inputs in this forum. Please help..

Code: Select all

#include<cstdio>
#include<cstdlib>
#include<algorithm>

using namespace std;

typedef struct list_t{
	unsigned long p;
	char c;
	char r[3];
	bool operator<(const struct list_t &a){
		return p > a.p;
	}
}list_t;

static list_t cnt[92];

static const char lut[128] = {
						0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
						0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
						0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
						0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
						0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
						0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
						0, 0, 0, 0, 0, 'A', 'B', 'C', 'D', 'E',
						'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
						'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
						'Z', 0, 0, 0, 0, 0, 0, 'A', 'B', 'C',
						'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
						'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
						'X', 'Y', 'Z', 0, 0, 0, 0, 0};

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
#endif

	for(int i = 'A'; i <= 'Z'; i++){
		cnt[i].c = i;
	}

	unsigned int N;
	scanf("%u\n", &N);
	register char c;
	while((c = getchar()) != EOF){
		if(lut[c]){
			cnt[lut[c]].p++;
		}
	}

	sort(cnt + 'A', cnt + 'Z' + 1);
	
	for(int i = 'A'; cnt[i].p; i++){
		printf("%c %lu\n", cnt[i].c, cnt[i].p);
	}

	return 0;
}
Post Reply

Return to “Volume 100 (10000-10099)”