sakhassan wrote:still no clue!!!!!!!!!!![]()
![]()
![]()
still RTE
ok , your "char hash[M][N]; " , if we increase the size of N to
1000000 , what will happen ??
![:D](./images/smilies/icon_biggrin.gif)
think about it , maybe you shoud find a better solution !
![:D](./images/smilies/icon_biggrin.gif)
Moderator: Board moderators
Code: Select all
#include <string>
#include <map>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <iostream>
using namespace std;
#define TSIZE 13881
typedef vector<map<string ,int> > HashTable;
void hash(string & sub, int & key1)
{
key1 = 0;
int i;
int t = 1;
int len = min<int>(5,sub.size());
t = 1;
for(i=0;i<len;i++)
{
t = (i<<1);
key1 += (((int)(sub[i])-97))<<t;
}
key1 = key1%TSIZE;
}
int insert(HashTable & HT, string & sub)
{
int key1;
hash(sub,key1);
map<string ,int>::iterator pos;
pos = HT[key1].find(sub);
if(pos == HT[key1].end())
{
HT[key1][sub] = 1;
return 1;
}
else
{
pos->second++;
return pos->second;
}
}
string search(int N, string & S)
{
int i,k;
string sub;
int T = 0;
int t;
string ret;
HashTable HT = HashTable(TSIZE+1);
sub = S.substr(0,N);
k = 0;
for(i=1;i<S.length()-N+1;i++)
{
sub.erase(sub.begin());
sub.append(1,S[i+N-1]);
t = insert(HT,sub);
if(T < t)
{
T = t;
k = i;
}
}
ret = S.substr(k,N);
return ret;
}
int main()
{
int N;
string buffer;
while(cin>>N>>buffer)
{
cout<<search(N,buffer)<<endl;
}
}
Code: Select all
I modified xlspace's code and got AC in 3.787 sec. It's possible to get AC using STL.rio wrote:The best cpu time of this problem is near 1sec, so I think it will be hard to avoid TLE using STL.
Actually, you can use 'long long' to represent a string instead. That makes things simplier.xlspace wrote:I get AC now,
first I not use substr function in string
second , I use pair<unsigned int, unsigned int> to represent a string.
Cool ideaDJWS wrote:Actually, you can use 'long long' to represent a string instead. That makes things simplier.
Maybe your runningtime is too slow?Darko wrote:This is what I do (n - number given (<=10), slen - length of the string):
- read string into an array (O(n))
- sort pointers to substrings (O(n*slen*log(slen)) <- bad?
- go through list of pointers and find the longest repeating sequence (O(n*slen))
Code: Select all
import java.io.IOException;
import java.util.StringTokenizer;
class Problem902 {
static String testCase, codeLine,password;
static StringTokenizer st;
static short N, m, max;
static char[] code, passwordVet;
static String[] allPasswords;
static short[] numPasswords;
public static void main(String[]args){
while(true){
input();
if(testCase == null)break;
input(testCase);
setArrays();
for(short i = 0; i <= (code.length-N); i++){
password = getPass(i);
checkPass(password);
}
System.out.println(findPass());
}
}
//Method to read from input, copied from problem100 sample solution
static String ReadLn (int maxLg)
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}
if ((car < 0) && (lg == 0)) return (null);
return (new String (lin, 0, lg));
}
//Method to read a line from the input
static void input(){
testCase = ReadLn(255);
}
//Method to convert the line read from the other method into a number (N) and a string(codeLine)
static void input(String linha){
st = new StringTokenizer(linha);
N = Short.parseShort(st.nextToken());
codeLine = st.nextToken();
}
//Method to convert codeLine into a char Array and to create 3 other arrays
static void setArrays(){
code = codeLine.toCharArray();
passwordVet = new char[N]; //stock a password to work with
allPasswords = new String[(code.length-(N-1))]; //stock all passwords already reads
numPasswords = new short[allPasswords.length]; //count how many times a certain password was read. it conects with allPasswords by using the same index (i.e. numpasswords[2] counts how many times the string in allPasswords[2] was read
}
//Method to get 3 chars in a row and convert to a String
static String getPass(short i){
for(short j = 0; j < N; j++, i++) passwordVet[j] = code[i];
String pass = new String(passwordVet);
return pass;
}
//Method to use a String and check if i has already been read
static void checkPass(String pass){
for(m = 0; m < allPasswords.length; m++){
if(allPasswords[m] == null){
allPasswords[m] = pass;
numPasswords[m]++;
break;
}
if(saoIguais(allPasswords[m], pass)){
numPasswords[m]++;
break;
}
}
}
//Method to seek allPasswords and numPasswords and return the most read password as answer for the test case
static String findPass(){
max = 0;
for(short i = 0; i < numPasswords.length; i++){
if(numPasswords[i] > max) max = numPasswords[i];
}
for(short i = 0; i < numPasswords.length; i++){
if(numPasswords[i] == max) return allPasswords[i];
}
return "";
}
//Method to compare 2 Strings. P.S. Don't know why, but using "if(a == b)" didn't work
static boolean saoIguais(String a, String b){
char[] aa = a.toCharArray();
char[] bb = b.toCharArray();
for(short i = 0; i < aa.length; i++){
if(aa[i] != bb[i])return false;
}
return true;
}
}
Code: Select all
Here are the compiler error messages:
05882640_24.java: In class `Main':
05882640_24.java: In method `main(java.lang.String[])':
05882640_24.java:21: Internal compiler error in `expand_expr', at expr.c:5844
Thanks for pointing that out. But the STL has optimised that so it is much more efficient than you trying to build a binary tree yourselfemotional blind wrote:but access map and insertion in map will cost O(logn), Sor your algorithm is not O(n) it is O(nlogn).kenneth wrote:The complexity of the algorithm is O(n) as you only need to go thru the string once followed by going thru the map once. Hope this helps.