Common Board| Show all threads Hide all threads Show all messages Hide all messages | | Accepted in c++ | WENXIANG LU | 1319. Hotel | 15 Jul 2017 13:53 | 3 | #include <iostream> int main() { int N; std:: cin >> N; /* dynamic memory allocation */ int ** p = new int * [N]; for (int i = 0; i < N; i++) p[i] = new int [N]; /* initialize upper triangle matrix */ p[0][N-1] = 1; int m, n; int ct = 1, value = 2, iter = 1, row = 0, column = N - 2; while (ct <= N - 1) { n = column, m = row; for (int i = 0; i <= iter; i++) { p[m++][n++] = value; value++; }
iter++; ct++; column--; } /* initialize the lower triangle matrix */ row = 1; column = 0, iter = N - 2; while (ct > 1) { n = column, m = row; for (int i = 0; i <= iter; i++) { p[m++][n++] = value; value++; } iter--; ct--; row++; } /* display matrix */ for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) std::cout << p[i][j] << " "; std::cout << std::endl; }
/* release memory */ for (int i = 0; i < N; i++) delete[] p[i]; delete[] p; return 0; } We do not give a fuck. remove the code. | | Acces Violation test #4 and after that get WA #4, C++. Anyone can help me? still stuck test #4 | VNeo | 1313. Some Words about Sport | 15 Jul 2017 13:03 | 4 | first i get acces violation in my code. and then i search what it acces violation, and then i remake my code, then submit again. still get WA #4, before i reamke the code it says Accces Violation in test #4. Can anybody help me? This is my code. #include <iostream> using namespace std; int n,ar[10000],p; int main(){ cin>>n; for (int i=1;i<=n*n;i++){ cin>>p; ar[p]=p; } for (int i=1;i<=n*n;i++){cout<<ar[i]<<' ';} } Edited by author 15.07.2017 10:58 Or I must use Array 2 dimensions? http://ideone.com/8vvdveIf you don't want runtime error #4 pay attention arrays in C/C++ are 0-indexing and enlarge your array by one for example If you want AC you can AC even with one-d array To do that you should write much more smart program than yours understood. thnks dude. Edited by author 15.07.2017 13:05 Edited by author 15.07.2017 13:05 | | WA#12 | jerry | 1297. Palindrome | 15 Jul 2017 12:31 | 3 | WA#12 jerry 15 Jul 2017 07:25 tried all mentioned test cases .all correct. still WA on #12. using surfix array.any idea? code here: #include <cstdio> #include <cstring> int const N=220000; int st[256], rank[2*N], rank1[2*N], count[N], tmp[N]; char a[N], b[N], s[N], max1; int sa[N], height[N], si; int main(){ memset(st, 0, sizeof st); memset(rank, 0, sizeof rank); memset(rank1, 0, sizeof rank1); scanf("%s", b); int n1=strlen(b); for(int i=1; i<=n1; ++i)a[i]=b[i-1]; for(int i=1; i<=n1; ++i)a[i+n1+1]=b[n1-i]; a[0]=' '; a[n1+1]='#'; int n=2*n1+1; a[n+1]='&'; for(int i=1; i<=n; ++i)st[a[i]]=1; for(int i=1; i<=255; ++i)st[i]+=st[i-1]; for(int i=1; i<=n; ++i)rank[i]=st[a[i]];
int k=0; for(int p=1; k!=n; p+=p){ memset(count, 0, sizeof count); for(int i=1; i<=n; ++i)count[rank[i+p]]++; for(int i=1; i<=n; ++i)count[i]+=count[i-1]; for(int i=n; i>=1; --i)tmp[count[rank[i+p]]--]=i;
memset(count, 0, sizeof count); for(int i=1; i<=n; ++i)count[rank[i]]++; for(int i=1; i<=n; ++i)count[i]+=count[i-1]; for(int i=n; i>=1; --i)sa[count[rank[tmp[i]]]--]=tmp[i];
memcpy(rank1, rank, sizeof rank1); rank[sa[1]]=k=1; for(int i=2; i<=n; ++i){ if(rank1[sa[i]]!=rank1[sa[i-1]] or rank1[sa[i]+p]!=rank1[sa[i-1]+p])++k; rank[sa[i]]=k; } } k=0; for(int i=1; i<=n; ++i){ if(rank[i]==1){ k=0; }else{ --k; if(k<0)k=0; while(a[i+k]==a[sa[rank[i]-1]+k])++k; } height[rank[i]]=k; }
int max=-1; for(int i=2; i<=n; ++i){ int p=sa[i]; int q=sa[i-1]; if((p-1)/n1 != (q-1)/n1){ if(p+q+height[i]==n+2){ if(height[i]>max){ max=height[i]; si=p; } } } } for(int i=si; i<si+max; ++i)printf("%c", a[i]); return 0; } THX :) Re: WA#12 Nikita Mogilevets 15 Jul 2017 09:29 Are interesting in solving the task or in solving the task with suffix array or in knowing why your program outputs wrong at #12? Probably third is the case... So I tried to read your program. Have not understand. Maybe you elaborate details of your algorithm. You need to output the first of them if there are several such strings.But when you correct it ,i guess you 'll WA #18 | | Sasuke failed? | Nikita Mogilevets | 1000. A+B Problem | 15 Jul 2017 11:00 | 1 | | | Двоичное дерево поиска: не могу найти ошибку в функции, которая удаляет вершину. Помогите пожалуйста. | Syavaprd | | 15 Jul 2017 08:59 | 4 | #include <bits/stdc++.h> #define pb push_back #define fs first #define sc second #define INF (1e9+7) #define forn(i,z,n) for( auto (i) = (z); (i) < (n); ++i) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; struct node { int key; node *left, *right, *parent; node(int q, node *x, node *y, node *p) { key = q; left = x; right = y; parent = p; } }; node *tree = NULL, *f = NULL; void add_n( node *j, node *&z) { if( z == NULL) { z = new node(j->key,NULL,NULL,NULL); return ; } if( z->key >= j->key) { if( z->left) { add_n(j, z->left); } else { z->left = j; j->parent = z; } } else { if( z->right) { add_n(j,z->right); } else { j->parent = z; z->right = j; } } } void show( node *x) { if( x != NULL) { show(x->left); cout << x->key << ' '; show(x->right); } else return ; } node *find_min( node *root) { if( root->left != NULL) { return find_min(root->left); } else { return root; } } void delete_n(node *root, int val ) { if( root == NULL) return ; else if( val < root->key) delete_n( root->left, val); else if( val > root->key) delete_n(root->right, val); else { if( root->left == NULL && root->right ==NULL) { //delete root; root = NULL; } else if( root->left == NULL) { root->right->parent = root->parent; root = root->right; //delete root->right; root->right = NULL; } else if( root->right == NULL) { root->left->parent = root->parent; root = root->left; //delete root->left; root->left = NULL; } else { node *temp = find_min( root->right); temp->parent = root->parent; root = temp; delete_n( root->right, temp->key); } } return ; } void post( node *x) { if( x != NULL) { post(x->right); post(x->left); cout << x->key << ' '; } } void show_parents( node *z) { if(z != NULL) { show_parents(z->left); show_parents(z->right); if( z->parent != NULL) cout << z->key << ' ' << (z->parent->key) << endl; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; vector <int> v; forn(i,0, n) { int x; cin >> x; v.pb(x); } for(int i = 0; i <n; ++i) { node *help = new node(v[i], NULL, NULL,NULL); add_n( help, tree); } show(tree); // в невозрастающем порядке delete_n(tree, 3); cout << endl; show(tree); return 0; } I have found one mistake and then stopped reading. If val==root key, you are assigning root=NULL. root is a not root of your tree, root is just a copy. So you are assigning NULL to a copy of a node to your tree, not a real node. Ну короче функция которая удаляет, как минимум, должна получать **root вместо *root. Может быть, еще что-то есть, я не дошел. Я как компилятор короче, дооел до первой строки в которой что-то не так и остановился | | WA#19 can you help me? | Sorn | 1786. Sandro's Biography | 15 Jul 2017 02:42 | 2 | #include<iostream> #include<string> using namespace std; int main () { string s; cin>>s; int max=0,i=0,index=0,a=0,p=0; for (i=0; i<s.size()-5; i++) { if(s[i]>'a' && s[i]<='z') p--; if(s[i]=='S') p+=2; if(s[i+1]=='a') p+=2; if(s[i+1]>='A' && s[i+1]<='Z') p--; if(s[i+2]=='n') p+=2; if(s[i+2]>='A' && s[i+2]<='Z') p--; if(s[i+3]=='d') p+=2; if(s[i+3]>='A' && s[i+3]<='Z') p--; if(s[i+4]=='r') p+=2; if(s[i+4]>='A' && s[i+4]<='Z') p--; if(s[i+5]=='o') p+=2; if(s[i+5]>='A' && s[i+5]<='Z') p--; if (p>max) { max=p; index=i; } p=0; } i=index; if (s[i]!='S' && s[i]>='A' && s[i]<='Z' || s[i]=='s') a+=5; else if (s[i]!='S' && s[i]>='a' && s[i]<='z') a+=10; if (s[i+1]!='a' && s[i+1]>='a' && s[i+1]<='z' || s[i+1]=='A') a+=5; else if (s[i+1]!='a' && s[i+1]>='A' && s[i+1]<='Z') a+=10; if (s[i+2]!='n' && s[i+2]>='a' && s[i+2]<='z' || s[i+2]=='N') a+=5; else if (s[i+2]!='n' && s[i+2]>='A' && s[i+2]<='Z') a+=10; if (s[i+3]!='d' && s[i+3]>='a' && s[i+3]<='z' || s[i+3]=='D') a+=5; else if (s[i+3]!='d' && s[i+3]>='A' && s[i+3]<='Z') a+=10; if (s[i+4]!='r' && s[i+4]>='a' && s[i+4]<='z' || s[i+4]=='R') a+=5; else if (s[i+4]!='r' && s[i+4]>='A' && s[i+4]<='Z') a+=10; if (s[i+5]!='o' && s[i+5]>='a' && s[i+5]<='z' || s[i+5]=='O') a+=5; else if (s[i+5]!='o' && s[i+5]>='A' && s[i+5]<='Z') a+=10; cout<<a<<endl; return 0; } Edited by author 30.12.2014 18:40 Edited by author 03.01.2015 17:13 Edited by author 03.01.2015 17:13 Try Dandro (must be 5, feeling like ur programm returns 10) | | To admins | __Andrewy__ | 1743. Domino Sorting | 14 Jul 2017 14:10 | 1 | 1)Look at my solution 7450309 (Runtime error (non-zero exit code) on test 23)) and 7450312(AC). Why i got Runtime error (non-zero exit code) on test 23? In first solution i have nmax=123456 and in second 223456. What is range of n? 2)Look on problem 1974. In forum i wrote test when my AC solution get wrong answer(I can write more tests where my old AC solution falls) | | How to solve. | Nikita Mogilevets | 1501. Sense of Beauty | 14 Jul 2017 10:39 | 1 | I have solved it by reduction to a graph exploration task. Each vertex consists of three parameters (i, j, pr). Where i, j denote number of card at the top of each pile and pr denotes previous state. pr is also a "used" array. So, if pr[N] [N] =none, then impossible. Otherwise just recover answer using pr. You can also have in your state additional parameter diff denotes difference between red and black cards. I have used instead additional two arrays diff11 and diff2 denote difference between first i cards in first pile and in the second pile respectively. There is an edge iff difference at the next step would be not greater than one (abs) , obviously. | | Why i get "Wrong answer"? | Sagamore | 2056. Scholarship | 13 Jul 2017 10:41 | 7 | //I have correct in the IDE, what's wrong? import java.io.IOException; import java.util.Scanner; public class VasyaCare{ public static void main(String[] args) throws IOException { VasyaCare vasya_care = new VasyaCare(); Scanner scn = new Scanner(System.in); int n = scn.nextInt(); try{ // Check if(n < 10){ double[] b = vasya_care.setScore(n); double median = vasya_care.getMedian(b); if(median==3){ System.out.println("None"); } else if(median==5){ System.out.println("Named"); } else if(median >= 4.5){ System.out.println("High"); } else if(median < 4.5){ System.out.println("Common"); } } else throw new IOException("Please, type number from 1 to 10!"); } catch(Exception e){e = new IOException();} } double getMedian(double[] b{ // calculate median score of exam int sum = 0; for(int i = 0; i < b.length; i++){ sum = (int) (sum + b[i]); } return (double)sum/b.length; }
double[] setScore(int n){ // forming list scores Scanner scn = new Scanner(System.in); double[] b = new double[n];{ for(int j = 0; j < n; j++){ int m = scn.nextInt(); if(m<3 || m > 5){ System.out.println("Type score from 3 to 5: "); j--; } else if (m>=3 && m<=5){ b[j] = m; } } } return b; } } // Thank you for your attention! If Vasya has at least one note 3 scholarship is 'None'. So, 355555555... is no scholarship. Sorry if you considered that, I have read your code very careless. You're wrong, sorry, there is my testing: Input Type number exam: 8 Type score: 3 Type score: 5 Type score: 5 Type score: 5 Type score: 5 Type score: 5 Type score: 5 Type score: 5 3.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 average score: 4.75 Output: High I really don't understand, what's wrong. (( I have just copied code from my Accepted submissions. http://ideone.com/vWEL58I think the problem is easy enough To let post AC code be legal. You have "none" because your case "cur=int (input ()) if cur==3:..." does not check for the average score and passes at runtime immediately. You must check the average score in all cases, not the input variable. Are you a troll? That is Accepted code. If you copy & paste you AC. So there correct answer is "None" and dialog interface is not needed. Moreover, in most cases,there should be no dialog interface to get AC. | | Get rid of WA#19 | Nikita Mogilevets | 1371. Cargo Agency | 13 Jul 2017 10:37 | 1 | Let unsigned long long total_cost=0. For every edge E in tree total_cost +=cost_of_all_paths_lies_on_edge(E). double ans=2.0*total_cost/n/(n-1) is overflow and and WA#19. ================ Let double ans = 0. For every edge E in tree ans+=2.0*cost_of_all_paths_lies_on_edge(E)/n/(n-1). That is AC. =========== Maybe if not multiply by two in the first scenario there is also AC, I have not tested. | | WA #5 | Faeton (Kyiv - Mohyla Academy) | 1320. Graph Decomposition | 12 Jul 2017 20:48 | 2 | WA #5 Faeton (Kyiv - Mohyla Academy) 4 Mar 2008 21:59 | | tests | taobingxue | 1393. Average Common Prefix | 12 Jul 2017 18:04 | 2 | tests taobingxue 13 Sep 2009 20:28 5 AAAAA ans:5.000 6 CBDCBD ans:3.600 6 DDADDD ans:2.000 They helped me to solve this problem~ Hope they can help you o(∩_∩)o... Re: tests Nikita Mogilevets 12 Jul 2017 18:04 Test "DDADDD" is really nice. My program shows different results for different shifts of that string. For first two shifts it shows 2.0 and for the next four it shows 1.4,1.8,2.2 and 1.2 respectively | | Hint | index | 1930. Ivan's Car | 12 Jul 2017 17:22 | 2 | Hint index 1 Dec 2016 15:08 0-1 bfs on graph with 2 * n verticles. Re: Hint Gilles Deleuze 12 Jul 2017 17:22 Used this idea, but instead I used another adj. list to store directions cin >> u >> v graph[u].push_back(v); direction[u].push_back(0); graph[v].push_back(u); direction[v].push_back(1); 0.046s AC Edited by author 12.07.2017 17:23 | | Algorithm | Freeuni: GioSag | 1706. Cipher Message 2 | 12 Jul 2017 12:19 | 15 | how to solve this problem? construct suffix tree for N times and then LCP or there is easier and faster way? Re: Algorithm UdSU: Abizyaev, Urbanovich, Kibardin 6 Apr 2009 20:01 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. Re: Algorithm UdSU: Abizyaev, Urbanovich, Kibardin 9 Apr 2009 21:42 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, Urbanovich, Kibardin 11 Apr 2009 00:39 Hardly. O(n + k) may have arbitrary coefficient. And no one said that solution had been submitted at Timus. 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. Could you explain how to use KMP here? Edited by author 22.02.2014 15:24 | | Suffix automata | s.egorov | 1706. Cipher Message 2 | 12 Jul 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 | | WA8 with kmp!!! | YuFeng | 1423. String Tale | 11 Jul 2017 23:44 | 2 | //#define LOCAL #include <stdio.h> #include <string.h> #include <stdlib.h> #define MAXN 250000+10 int N; char str1[MAXN], str2[MAXN]; char temp[MAXN*2]; int kmp(char* s, char* t, int pos); void get_next(char* t, int* next, int t_len); int main() { #ifdef LOCAL freopen("1423_String_Tale_2.in", "r", stdin); freopen("1423_String_Tale_2.out", "w", stdout); #endif scanf("%d", &N); scanf("%s%s", str1, str2);
int s1_len = strlen(str1); int s2_len = strlen(str2);
if(s1_len != s2_len){ printf("%d\n", -1); return 0; }
strcpy(temp, str1); strcat(temp, str1); int ans; ans = kmp(temp, str2, 0); if(ans != 0 && ans != -1) printf("%d\n", s1_len-ans); else printf("%d\n", ans); return 0; } int kmp(char* s, char* t, int pos) { int s_len = strlen(s); if(pos<0 || pos>s_len-1){ fprintf(stderr, "%s", "Error: invalid pos."); system("pause"); }
int t_len = strlen(t); int *next = (int*)malloc(sizeof(int) * t_len); get_next(t, next, t_len);
int i = pos; int j = 0; while(i<s_len && j<t_len){ if(j<0 || s[i]==t[j]){ i++; j++; } else j = next[j]; }
// free(next); if(j >= t_len) return i-t_len; else return -1; } void get_next(char* t, int* next, int t_len) { next[0] = -1; int j = 0; int k = -1; while(j < t_len){ if(k==-1 || t[j]==t[k]){ j++; k++; next[j] = k; } else k = next[k]; } } ############################################### Can anyone tell me WHY??? Edited by author 11.07.2017 19:31 Maybe you don't process characters with negative ASCII correctly. I don't know how standard string functions deal with negative ASCII. | | =( | 1234321 | 1713. Key Substrings | 11 Jul 2017 18:06 | 10 | =( 1234321 11 Oct 2009 21:45 Please, give me some hints on how to solve it. Thanks in advance. Re: =( Vedernikoff 'Goryinyich' Sergey (HSE: АОП) 12 Oct 2009 23:35 Re: =( Igor9669 12 Oct 2009 23:57 I also use hash,but TLE 3 =( My complexity is O(N*MaxLen^2*logN). What is your? Edited by author 13.10.2009 00:06 Re: =( Victor Barinov (TNU) 13 Oct 2009 15:47 Try use hash table to avoid logN Re: =( Vedernikoff 'Goryinyich' Sergey (HSE: АОП) 14 Oct 2009 16:55 My complexity is O(N^2*M^2*(logN+logM)), where M is maximal length of a word. It passes TL due to small constant. P.S. You don't need hash tables to avoid N. Just one linear hash and sorting. Edited by author 14.10.2009 20:41 Re: =( Igor9669 16 Oct 2009 12:41 Re: =( Nikita Mogilevets 11 Jul 2017 16:21 (N^2)*(M^2)*(logN+logM)? When N=1e3 and M=1e2? Accepted? Anybody knows how it was done? Edited by author 11.07.2017 16:23 Re: =( Nikita Mogilevets 11 Jul 2017 18:06 So, I understood If explicitly sort suffixes of string command1+command command2+command3+... we have upper bound N^2*M^2 but actual number of comparisons is smaller Re: =( Roks.second 5 Apr 2010 07:58 This problem can be very simply solved using standard techniques of suffix array + lcp array. Complexity is O(N * M * logN), M - length of the longest string. Due to all-round using of STL my solution works in 0.25, but this time can be hugely improved. SkorKNURE Edited by author 05.04.2010 08:00 Re: =( coder 15 Jan 2012 23:01 This problem can be very simply solved using standard techniques of suffix array + lcp array. Complexity is O(N * M * logN), M - length of the longest string. Due to all-round using of STL my solution works in 0.25, but this time can be hugely improved. SkorKNURE Edited by author 05.04.2010 08:00 You are right! I Ac! 0.046 s. Thanks! | | std::deque if you want an easy AC | Gilles Deleuze | 1640. Circle of Winter | 11 Jul 2017 16:05 | 3 | What? Sandro don't require to move at all... So I don't understand why use deque while we can solve it with simple max(a, b) | | Clang C++14 Compiler Failed | Nikita Mogilevets | 1517. Freedom of Choice | 10 Jul 2017 19:55 | 1 | I have got a verdict Compiler Failed on Clang compiler. Never have got such a verdict. Every other compiler submit gets AC. Can't post code here because it is a working solution. | | Don't waste your time to binary search without hash | Nikita Mogilevets | 1713. Key Substrings | 10 Jul 2017 19:06 | 2 | I was trying to do the following. Construct suffix automation for each command. Binary search for length of each key substring. Binary search works as follows. For each substring of command of len (lower_bound+upper_bound) /2 check whether it is a substring of another command. And it was Time Limit Exceed #7. So, don't repeat yourself. Find better solution. Probably with suffix array, as posted on the webboard. |
|
|