My Weblog

Blog about programming and math

SPOJ 9122. Diary

SPOJ 9122. Diary is just simulation problem. Only thing we need to consider is ” If the decryption is not possible because there are multiple distances conforming to the rules above, print NOT POSSIBLE instead”. Count all the occurrence of alphabets and find out which one is maximum. If there are more than one alphabets , clearly its impossible to decrypt otherwise assign it ‘E’ and calculate distance and decrypt the string. Accepted c++ code.

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;

int cnt[30];

int main()
	{
		int t ;
		string s , final ;
		for( scanf("%d\n",&t) ; t > 0 ; t-- )
		{
			memset( cnt , 0 , sizeof cnt );
			getline( cin , s ); 
			int len = s.length();
			for(int i = 0; i < len ; i++) if ( s[i] >= ' ' ) cnt[s[i]-'A']++ ;
		
			//cout<<s<<endl;
			//for(int i=0;i<26;i++) cout<<cnt[i]<<" ";cout<<endl;	
			//find out most occuring word and this is E
			//what if i have more than two words have same occurance 
			
			int mx = -1 , ind = -1  ;
			for(int i = 0 ; i < 26 ; i++ ) if ( cnt[i] > mx ) mx = cnt[i] , ind = i ; 
			
			int c = 0 ; 
			for(int i = 0 ; i < 26 ; i++) if (cnt[i]==mx ) c++;
			if ( c>1 ) { cout<<"NOT POSSIBLE\n" ; continue ;} 
			
			int dist =  ( ind - 4 + 26 ) % 26 ;			
			final = "";
			for(int i = 0 ; i < len ; i++) 
				if (s[i] !=' ') final += ( char ) (  'A' + ( ( s[i] - 'A' - dist + 26 ) % 26 ) ) ; 
				else final += ' ' ;
			
			cout<<dist<<" "<<final<<endl;

		}
	}
Advertisements

July 7, 2011 - Posted by | Programming | , ,

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: