Guest User

Untitled

a guest
May 20th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. void recursivePrinting(int* arr, int* sequenceIndexs, int index){
  6. int nextIndex = sequenceIndexs[index];
  7.  
  8. printf("%d ", arr[index]);
  9.  
  10. if (sequenceIndexs[nextIndex] == -1) {
  11. return;
  12. }
  13. recursivePrinting(arr,sequenceIndexs, nextIndex);
  14.  
  15. }
  16.  
  17. void printfLongestIncreasingSequence(int* arr, int* sequenceIndexs, int size){
  18. int currentIndex = size - 1;
  19. while (sequenceIndexs[currentIndex] == -1) {
  20. currentIndex--;
  21. }
  22. recursivePrinting(arr,sequenceIndexs, currentIndex);
  23. }
  24.  
  25. int main(){
  26. int arr[] = {0,4,12,2,10,6,9,13,3,11,7,15};
  27. int size = sizeof(arr)/sizeof(int);
  28. int longestSubSequencesLenghts[size];
  29. int longestSubSequence[size];
  30. int i;
  31. for(i = 0; i < size; i++){
  32. longestSubSequencesLenghts[i] = 1;
  33. longestSubSequence[i] = -1;
  34. }
  35.  
  36. int j;
  37. for (i = 1; i < size; i++) {
  38. for(j = 0; j < i; j++){
  39. if (arr[j] < arr[i]) {
  40. int currentLenght = longestSubSequencesLenghts[i];
  41. int newLenght = longestSubSequencesLenghts[j]+1;
  42. if (currentLenght > newLenght) {
  43. longestSubSequencesLenghts[i] = currentLenght;
  44. } else {
  45. longestSubSequencesLenghts[i] = newLenght;
  46. longestSubSequence[i] = j;
  47. }
  48.  
  49. }
  50. }
  51. }
  52. puts("");
  53. int x;
  54. for(x = 0; x < size; x++){
  55. printf("%d ", longestSubSequencesLenghts[x]);
  56. }
  57.  
  58. puts("");
  59. for(i = 0; i < size; i++){
  60. printf("%d ", longestSubSequence[i]);
  61. }
  62. puts("************************************************");
  63.  
  64. printfLongestIncreasingSequence(arr,longestSubSequence,size);
  65. return 0;
  66. }
Add Comment
Please, Sign In to add comment