Показать все ветки Спрятать все ветки Показать все сообщения Спрятать все сообщения |
Did anyone solved this using rolling hash and unordered_map (HashMap)? | prituladima | 1706. Шифровка 2 | 9 апр 2024 12:02 | 1 |
I tried this approach with Java: Test 5. TL With C++: Test 9. TL And seems like this is not enough, however time complexity is O(αk^2 + α|S|*k) where α is hash map hidden constant. So... did anyone managed to solve it this way? |
TL5 if you use hash | andreyDagger | 1706. Шифровка 2 | 16 фев 2022 23:01 | 1 |
Try std::unordered_map instead of std::map. This one helped me |
Algorithm | Tbilisi SU: Giorgi , Nadira , [Kakia] | 1706. Шифровка 2 | 12 июл 2017 12:19 | 15 |
Algorithm Tbilisi SU: Giorgi , Nadira , [Kakia] 6 апр 2009 14:37 how to solve this problem? construct suffix tree for N times and then LCP or there is easier and faster way? Construct suffix tree for each window. Sliding should be at most O(k). Try DP. It's easier to code (+ short) and run much faster than using suffix tree although having the same complexity O(N*K). Could you please decribe me DP solution? Stupid implementation of the hash-table is even easier to code and much easier to invent. Coding is not difficult at all. It seems to be more interesting that there exists O(n + k) solution, isn't it? O(n=4000 + k=1000) would work 0.001ms ;) Re: Algorithm UdSU: Abizyaev, Bikov, Urbanovich 11 апр 2009 00:39 Hardly. O(n + k) may have arbitrary coefficient. And no one said that solution had been submitted at Timus. Re: Algorithm Ivanov Alexander (HSE Mozgless Eagles) 24 июл 2011 21:43 And what is the "stupid implementation of the hash-table"? My stupid implementation got TL. Probably DP solution is actually a suffix automation. In suffix automation, we have the following DP. Let X is some state of DAWG. Let DP[X] =1+sum(DP[Y]){there is an edge from X to Y in DAWG}. Then the answer is DP [ROOT] - 1. (Because empty substring not counts) Edited by author 12.07.2017 12:20 AC in 0.109 using only Z-function (also called suffix-function) Z-function is not necessary here, KMP is enough. 0.14 using KMP. Re: Algorithm Ivanov Alexander (HSE Mozgless Eagles) 25 июл 2011 00:41 Could you explain how to use KMP here? Edited by author 22.02.2014 15:24 |
Suffix automata | SergeiEgorov | 1706. Шифровка 2 | 12 июл 2017 10:28 | 3 |
Is it possible to solve this problem using suffix automata? I got TL5. But I think that O(N * K) solution have to work faster. Am I wrong? Thanks. PS. Got AC with KMP O(N * K). Edited by author 26.08.2015 00:30 Just got AC with suffix automation. 0.95 sec, 1.5MegaBytes. Nothing tricky, just avoided STL. Construct an automation for every window and find number of different paths. I think my program is pretty slow because when I am constructing an automation I am using modulo operation every step (before adding every new symbol) to deal with cyclic shift. Obviously it can be improved by checking whether i+k>len(s) and branching. Also my program is slow because I am allocating and deallocating an entire array for DAWG for every window. Obviously it can be allocated once |
using dc3 algorithm | akos tajti | 1706. Шифровка 2 | 15 июн 2014 17:26 | 1 |
finally I solved this using DC3 algorithm (a linear suffix array construction algoritm). it's supposed to run in O(n) time. my java solution runs in ~1.17 but my main language is scala and in scala (the escat same algorithm) is above 2 sec. is it possible to solve this with KMP faster? i couldn't find a correct solution. |
good idea !!! | AlisheR Tojiev [☆UZB★TUIT☆] | 1706. Шифровка 2 | 28 мар 2014 01:18 | 3 |
aabaaab => { 0 1 0 1 2 3 } a 0 aa 1 aab 0 aaba 1 aabaa 2 aabaab 3 abcabcd a 0 ab 0 abc 0 abca 1 abcab 2 abcabc 3 abcabcd 0 0 0 0 1 2 3 0 |
one test | orbby | 1706. Шифровка 2 | 4 мар 2012 21:22 | 1 |
6 abcabcabcf 15 15 15 15 18 19 18 18 19 18 |
Wrong Answer Test#2 | Topravy | 1706. Шифровка 2 | 4 мар 2012 20:07 | 1 |
Hi, Please let me know the input for Test#2. I have verified my program with different test cases and is working fine. It would be interesting to see which test case is failing. |
suffix automata | jlcastrillon | 1706. Шифровка 2 | 27 авг 2011 03:50 | 2 |
Could anyone tell me how to solve this task using suffix automata?? I have already found a way to solve this problem using suffix automata, solution would yield to an O(n^2) time, it's just a simple calculation. KMP also works efficiently here. |
A test | wRabbits_AlMag(VNTU) | 1706. Шифровка 2 | 21 июл 2011 20:22 | 1 |
A test wRabbits_AlMag(VNTU) 21 июл 2011 20:22 try 6 lmffwmnrocz 19 19 20 21 21 21 21 21 21 20 20 |
Faster than using suffix array? | Vlad | 1706. Шифровка 2 | 2 сен 2010 18:17 | 1 |
I naively applied suffix arrays with lcp and got AC in ~2.7s. (complexity N*K*log^2(K)) But I see that there are submissions with times <0.1s. What are the more efficient algorithms? Someone mentioned suffix-function - any referece for that? Also it was said that KMP can be used - can anyone explain how exactly it is applicable here? Thanks in advance. |
it answers perfectly right on my machine ! | Esmaeil Ashrafi | 1706. Шифровка 2 | 1 апр 2010 12:51 | 4 |
I don't know whay this system returnes "wrong answer" !!! I ran my program several times,both in windows console and Netbeans IDE and every time i got the expected result. but here neither sending source file nor pasting source code doesnt accept ! and here is the code,if anyone interested : /* * this class implements the encoding requested in problem #1706 */ /** * @author Esmaeil Ashrafi <s.ashrafi@gmail.com> */ import java.util.ArrayList; public class StierlitzCipher { private static String input; // the string suppose to be ciphered private static int k; // the length of sub strings // temporary variable for query substrings private static StringBuffer temp = new StringBuffer(); // an array to store substrings of each query string private static ArrayList<String> sub = new ArrayList<String>(); /** * @param args the command line arguments */ public static void main(String[] args) { /* if ran in command line console,first argument willl be the * string to cipher and second considered as the key */ if (args.length == 2) { input = new String(args[1]); k = Integer.parseInt(args[0]); } // otherwise use of default values of problem else { input = new String("abaccc"); k = 3; // prompt the user to using default values System.out.println("doing encrypt by default values:" + "\nString : \"abaccc\"\nKey : 3"); } query(input, k); System.out.println(); // just for trimming the output ! }//end of main /** * gets the entire string and splits it to substrings in length * of "len" and sends each substring to method compute * for calculating the number of different non-empty substrings * could obtained from them.there will be substrings(and * consequent call to compute method)call as much as the length * of parameter str * @param str - the string to be cipher * @param len - the length of substrings(the key) */ private static void query(String str, int len) { int m, strLen = str.length(); for (m = 0; m <= strLen - len; m++) { compute(str.substring(m, m + len)); } for (m = m; m < strLen; m++) { compute(str.substring(m).concat(str.substring(0, len - (strLen - m)))); } }//end of query /** * queries queryString to calculate number of its different and * non-empty substrings and prints the count to the standard output * @param queryStr - the string to be queried */ private static void compute(String queryStr) { int qLen = queryStr.length(); for (int i = 0; i < qLen; i++) { for (int j = i + 1; j <= qLen; j++) { if (!sub.contains(temp.replace( 0, temp.length(), queryStr.substring(i, j)).toString())) { sub.add(temp.toString()); } } } System.out.print(" "+sub.size()); sub.clear(); }//end of compute }//end of class Edited by author 31.03.2010 05:29 1. Read FAQ. Programs should read and write data using standard input and output. You shouldn't use command line arguments to input data. 2. Don't output any debugging info and user prompt, like this: System.out.println("doing encrypt by default values:" + "\nString : \"abaccc\"\nKey : 3"); Thank you but there are more than 20 times i attempt to make my prog acceptable and EveryTime i got that "time limit exceeded". I have no idea after changing my code to the following (and this is just the last,i used any combination of String,StringBuffer you would think) how can make an acceptable answer while since the first time i ran my program i got the right answer ( but seems too late! ) And here is the last source code(the strategy is same as the first) : import java.io.*; public class StierlitzCipher2 { String input; // the string suppose to be ciphered int key; // the length of sub strings PrintWriter out ; StreamTokenizer in ; CharSequence temp ; public static void main(String[] args) throws IOException { new StierlitzCipher2().run(); } void run() throws IOException {
in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); in.nextToken(); key = (int) in.nval; in.nextToken(); input = in.sval; out = new PrintWriter(new OutputStreamWriter(System.out)); query(input, key); out.flush(); }//end of run void query(String str, int len) { int m, strLen = str.length(); for (m = 0; m <= strLen - len; m++) { compute(str.subSequence(m, m + len)); } for (m = m; m < strLen; m++) { compute(str.substring(m).concat(str.substring(0, len - (strLen - m)))); } }//end of query void compute(CharSequence queryStr) { int qLen = queryStr.length(), subCounter = 0; CharSequence[] sub = new CharSequence[(qLen * (qLen + 1)) / 2];
for (int i = 0; i < qLen; i++) {
for (int j = i + 1; j <= qLen; j++) { breakPoint :{ temp = queryStr.subSequence(i, j); for (int k = 0; k < subCounter; k++) { // comparing the temp to elements of sub if (sub[k].equals(temp)) { subCounter--; break breakPoint ; } } sub[subCounter] = temp ;} ++subCounter; } } out.print(subCounter); out.print(" "); }//end of compute }//end of class Does anyone have any idea how to make it ? Shouldn,t i use String objects (nor CharSequence) and please dont talk about StringBuffer,i used that ... Thanks in advance Edited by author 01.04.2010 13:23 and here is the third version : package stierlitzCipher; // in this version i use String and String[] in method compute // instead of StringBuffer and Array<String> // and iteration in array "sub" is more more sufficent : // it doesn't itrate whole array,just compares the temporary substring // to elements with same length import java.io.*; public class StierlitzCipher3 { String input; // the string suppose to be ciphered int key; // the length of sub strings PrintWriter out; StreamTokenizer in; public static void main(String[] args) throws IOException { new StierlitzCipher3().run(); } void run() throws IOException { in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); in.nextToken(); key = (int) in.nval; in.nextToken(); input = in.sval; out = new PrintWriter(new OutputStreamWriter(System.out)); query(input, key); out.flush(); }//end of run void query(String str, int len) { int m, strLen = str.length(); for (m = 0; m <= strLen - len; m++) { compute(str.substring(m, m + len)); } for (m = m; m < strLen; m++) { compute(str.substring(m).concat(str.substring(0, len - (strLen - m)))); } }//end of query void compute(String queryStr) { int subCounter = key, begin; String[] sub = new String[3*((key * (key + 1)) / 2)/2]; String temp; // initially inserting key subs as the least count for (int i = 1; i < key; i++) { sub[i-1] = (queryStr.substring(0, i)); } for (int i = 1; i < key; i++) { for (int j = i + 1; j <= key; j++) { temp = queryStr.substring(i, j); // comparing the temp to proper elements of sub begin = j - i-1 ; breakPoint: { do { if (temp.equals(sub[begin])) break breakPoint; } while (sub[(begin += key - 1)] != null); ++subCounter; sub[begin] = temp; } } } out.print(subCounter); out.print(" "); }//end of compute }//end of class /****************************************/ And(Again) the result is : time limit exceeded :( I don't know the problem is with algorithm i use or something about java i shoul consider... I think i should just leave it for the moment ! But i will be so thankful if anyboby guide me,cos i know there are submissions accepted programmed in java for this problem. This problem i walking on my nerve !!! Edited by author 01.04.2010 12:52 |
Wrong answer 2... Please help (Code in here) | georgi_georgiev | 1706. Шифровка 2 | 2 окт 2009 20:51 | 1 |
#include <iostream> #include <string> #include <algorithm> using namespace std; string a; int k; int array[9096], n, bucket[9096], lbucket[9096], h = 0; void scan(){ cin >> k >> a; a += a.substr(0, k - 1); n = a.size(); } bool f(int x, int y){ if ( !h ) return a[x] < a[y]; if ( bucket[x] < bucket[y] ) return 1; if ( bucket[y] < bucket[x] ) return 0; int bx, by; if ( x + h >= n ) bx = n - (x + h) - 1; else bx = bucket[x + h]; if ( y + h >= n ) by = n - (y + h) - 1; else by = bucket[y + h]; return bx < by; } bool makebuckets(){ int br = 0; for ( int i = 0; i < n; ++i ){ if ( i > 0 ) if ( f(array[i], array[i - 1]) || f(array[i - 1], array[i]) ) ++br; lbucket[array[i]] = br; } for ( int i = 0; i < n; ++i ) bucket[i] = lbucket[i]; ++br; return (br == n); } void buildsuffixarray(){ for ( int i = 0; i < n; ++i ) array[i] = i; sort (array, array + n, f); if ( makebuckets() ) return; for ( h = 1;; h *= 2 ){ sort(array, array + n, f); if ( makebuckets() ) break; } } int findnext(int st, int i){ ++i; for (i; i < n; ++i) if ( array[i] >= st && array[i] < st + k ) return i; return -1; } int rez(int t){ int p = findnext(t, -1), d, result = (k + 1) * k / 2; d = findnext(t, p); int br = 0; while ( d > 0 ){ int j = 0; while ( a[array[p] + j] == a[array[d] + j] && array[p] + j < t + k && array[d] + j < t + k ) ++j; result -= j; p = d; d = findnext(t, d); ++br; } if ( br != k - 1 ) while(1); return result; } void solve(){ buildsuffixarray(); cout << rez(0); for ( int i = 1; i < n - k + 1; ++i ) cout << " " << rez(i); cout << endl; } int main(){ scan(); solve(); } I am getting mad... no idea where my code is wrong... The idea is right |
WA#2 please help! | Smusenok Sergiy Andriyovich (KhAI) | 1706. Шифровка 2 | 17 сен 2009 23:06 | 2 |
Did you find what was the test? I have Wa2 too! Please s.o. help |
Help me | jhon | 1706. Шифровка 2 | 17 май 2009 05:01 | 1 |
please give me information of the test 2 of this problem. |