|
|
Общий форумplease give the code to java please give the code to java Edited by author 18.11.2017 07:25 #include<iostream> #include<cstdio> using namespace std; int main() { int n, a = 1, counter = 0; string s; cin >> n; for (int i = 0; i < n; ++i) { cin >> s; if (s[0] == 'A' || s[0] == 'P' ||s[0] == 'O' ||s[0] == 'R' && a == 2){ a = 1; counter++;} else if (s[0] == 'A' || s[0] == 'P' ||s[0] == 'O' ||s[0] == 'R' && a == 3){ a = 1; counter +=2;} if (s[0] == 'B' || s[0] == 'M' ||s[0] == 'S' && a == 1){ a = 2; counter++;} else if (s[0] == 'B' || s[0] == 'M' ||s[0] == 'S' && a == 3){ a = 2; counter++;} if (s[0] == 'D' || s[0] == 'G' ||s[0] == 'K' ||s[0] == 'T' || s[0] == 'W' && a == 1){ a = 3; counter +=2;} else if (s[0] == 'D' || s[0] == 'G' ||s[0] == 'K' ||s[0] == 'T' || s[0] == 'W' && a == 2){ a = 3; counter++;} } cout << counter; return 0; } You list D, G, K, T, W, but not J. initially let result sequence to be 10* 10* 10* 0* means zero or more zeroes. every time choose two number and if we merge them produce a smaller number then we merge them. the procedure stops until we can't merge to get a smaller number is it correct?? seems to be.. Edited by author 16.11.2017 07:10 we should modify this algo: every time choose right most two number that we merge them can get a smaller number then we merge them,.... we must merge numbers from right to left.... seems to be more correct Anyone crash test#2 Test #2 contains graph with only one node aaaaa aaa No aaa aaaaa aaaaaaaaa No aaaa aaaaa aabaa aabaabaabaaaaabaa No aab aab aaba aa aabaa aabaa aabaabaab No aab aab aab abcabcabccs abcaabcaabcaa No abca abca abca a aabaa aabaabaaaa No aab aabaa aa What's wrong? I have 'time limit excedeed' in 9th test. import functools def pr(n): result = [] i = 2 now = 1 while n >= 1: if n % i == 0: now *= i n //= i elif n % i != 0 and now > 1: result.append(now) i += 1 now = 1 else: i += 1 if n == 1: result.append(now) return result def f(n): result = 0 i = 1 while i ** 2 < n: if n % i == 0: result += 1 i += 1 if i ** 2 == n: return 2 * result + 1 else: return 2 * result newList = [int(input()) for _ in range(10)] product = functools.reduce((lambda x, y: x * y), newList) print(functools.reduce(lambda x, y: x * y, map(f, pr(product))) % 10) You don't must develop hard solution. Your work should not payed many time. Just pay attention, and look again on tests. Attention will save your time. I am sorry, for my poor English. Good luck! Edited by author 18.10.2015 16:52 Нужен максимум из 4-х минимумов) Edited by author 11.11.2017 22:53 Edited by author 11.11.2017 22:53 I was trying to write TRIE for mnemonic words and DFS that tree with phone number. And I have failed. After that, I wrote just 2-D dimensional dynamic programming solution. DP[i] [j] is minimal number of words to memorize substring [i;j]. And I got success. Used Python. Just input, not stdin from sys. i think you only need 1 array instead of dp[i,j] So probably there is jury solution available on the internet So I have examined jury solution Looks too simple Can't believe it is able to solve the problem where do you find the jury solution? using System; namespace t1068{ class Program{ public static void Main(string[] args){ int n = int.Parse(Console.ReadLine()); int i = 0; while (n != 1){ if (n >= 1){ i+= n; n--; } else if (n <= 0){ i+= n; n++; } } Console.Write(i + 1); //Console.ReadKey(); } } } using System; class Puzzle { public static void Main() { var n = int.Parse(Console.In.ReadLine()); (int start, int end) = n > 1 ? (1, n) : (n, 1); int sum = 0; for (int i = start; i <= end; i++) sum += i; Console.WriteLine(sum); } } static int Proccess(int input) { if (input == 1) { return 2; } else if (input == 0) { return 1; } bool isInputNegative = false; if (input < 0) { input = input * -1; isInputNegative = true; } int sum = 0; for (int i = 1; i < input + 1; i++) { sum += i; } if (isInputNegative) { sum = sum * -1; sum++; } return sum; } more understandable :D Edited by author 11.11.2017 20:22 time 0.078 memory 3 350 kb. I use triplex tree. It is very interesting problem. I used plain arrays and got AC in 0.093 and 410 KB. Solution with std::set and hand-written memory allocator gets AC in 0.062 and 534 KB. Where I can find info about triplex tree? I used google, but nothing found... time 0.078 memory 3 350 kb. I use triplex tree. It is very interesting problem. #include <stdio.h> #include <iostream> using namespace std; int n, m, segRez[3004]; int segm1[3004]; char segm2[6005]; int main() { cin >> n >> m; for(int i = 0; i < n; ++i){ gets_s(segm2); for(int j = 0; j < m; ++j){ segRez[j] = segm1[j] + int(segm2[j<<1] - 48); segm1[j] = int(segm2[j<<1] - 48); if(j - 1 >= 0 && ((segRez[j - 1]|segRez[j])) == 3){ cout << "No"; return 0; } } } cout << "Yes"; } Here is my java code : import java.util.Scanner; public class Example{ public static void main(String []args){ Scanner in = new Scanner(System.in); int number1,number2,number3; int [] array; int position=0; boolean check=false; int sth; number1=in.nextInt(); number2=in.nextInt(); number3=in.nextInt(); array = new int[number2]; for(int i=0;i<=number2-1;i++){ sth=(int)Math.pow(i,number1); if((sth-number3)%number2==0){ array[position]=i; position++; check=true; }else if(i==number2-1 && check==false){ System.out.println(-1); System.exit(0); }
} for(int p=0;p<position-1;p++){ for(int j=p+1;j<position;j++){ if(array[p]>array[j]){ int t=array[p]; array[p]=array[j]; array[j]=t; } } } for(int i=0;i<position;i++){ System.out.print(array[i] + " "); } System.out.println(); } } I get an error on test#3 . And on my C++ solution i get an error on test#6. #include<iostream> #include<cmath> using namespace std; int main(void){ unsigned short int N,M,Y; cin>>N>>M>>Y; unsigned short int X[M]; unsigned short int position=0; bool some_check=false; int sth; for(int i=0;i<=M-1;i++){ sth=pow(i,N); if((sth-Y)%M==0){ X[position]=i; position++; some_check=true; } else if(i==M-1 && some_check==false){ cout<<-1<<endl; return 0; } } for(unsigned short int i=0;i<position-1;i++){ for(unsigned short int j=i+1;j<position;j++){ if(X[i]>X[j]){ swap(X[i],X[j]); } } } for(unsigned short int i=0;i<position;i++){ cout<<X[i]<<" "; } cout<<endl; return 0; } Please,any ideas ??? int main() { int n; string s1, s2; cin >> n >> s1 >> s2; s1 += s1; int pos = (int) s1.find(s2); switch ( pos ) { case -1: cout << -1; break; case 0: cout << 0; break; default: cout << n-pos; } return 0; } Unfortunately, your solution has been rejudged. :) string.find() is too slow. Good luck! ¿Any idea of why a code can pass all the tests except the test 6 ? I prove my code with this imput, and it works fine 7325189087 5 it your reality real our 4294967296 5 it your reality real our 213456 12 id a h lm aid aidhlm hl m dhlm ai aid hlm 2017697240238123468420176972402381234684201769724023812346842017697240238123468420176972402381234684 7 azipmwpbgqadtjafhmth azipmwpbgqadtjafhmth azipmwpbgqadtjafhmth azipmwpbgqadtjafhmth azipmwpbgqadtja azipmwpbgqadtjafhmthazipmwpbgqadtjafhmthazipmwpbgqadtjafhmthazipmwpbgqadtjafhmth fhmth 27386428376 0 97854668689678768 3 dfgdfhjghjdggzash dfgkjt nkhhdfsge 11111111111 5 ij jj ji j i 1234567890 5 bdh txo bdhkmp i kmp -1 the answer is : reality our No solution. aidhlm azipmwpbgqadtjafhmth azipmwpbgqadtjafhmthazipmwpbgqadtjafhmthazipmwpbgqadtjafhmthazipmwpbgqadtjafhmth No solution. No solution. ji ji ji ji ji i i bdhkmp txo This is my code: import time import sys sys.setrecursionlimit(100000) n = int(input("Enter N (for digits): ")) k = int(input("Enter K: ")) rangeVar = pow(k, n) - pow(k, n-1) starter = pow(k, n) - rangeVar counter = 0 start = time.clock() def calc(i): global starter, rangeVar, counter, n, k temp = str(i)
if i == pow(k, n): return counter else: for j in range(len(temp) - 1): if temp[j] == '0' and temp[j+1] == '0': counter += 1 calc(i+1)
calc(starter) end = time.clock() print(rangeVar - counter) print(end - start) Remove repetitions before applying your algo i.e.: sequences like 2 1 2 2 2 2 1 change into 2 1 2 1 It helped me with WA#21 Yep. For understanding why, lets consider: 1 7 3 2 1 4 4 5 They should be 7 numbers in the list! |
|
|