fork(1) download
  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4. char a[100000],te='`';
  5. long long int i,j,k[10000],kp=0,l,co=0,n,ap=0,np=0,y,temp,fl,o;
  6. bool isChar(char a)
  7. {
  8. if((a>='a')&&(a<='z')){return true;}
  9. else return false;
  10. }
  11. int main()
  12. {
  13. cin>>n; //Take in the number of rows for the input
  14. for(i=0;i<=n;i++) //Loop for taking in the sentence and ignoring the punctuations(they arent printed in the sample output)
  15. {
  16. while(te!='\n')
  17. {
  18. cin.get(te);
  19. if((te=='\n')&&(a[ap]!=' '))
  20. {
  21. a[ap]=' '; ap++;
  22. break;
  23. }
  24. else if(((te>='a')&&(te<='z'))||((te>='A')&&(te<='Z')))
  25. {a[ap]=te; ap++;}
  26. else if(te=='\''){}
  27. else
  28. {
  29. if(a[ap-1]!=' '){a[ap]=te; ap++;}
  30. }
  31. }
  32. te='`';
  33. }
  34. for(i=0;a[i]!='\0';i++) //Converting all characters to lower case and storing length of array in i
  35. {
  36. if((a[i]>='A')&&(a[i]<='Z')){a[i]+=32;}
  37. }
  38. if((a[0]>='a')&&(a[0]<='z')) //Checking the first letter (corner case for my algo
  39. {
  40. k[0]=0; kp++; co++;
  41. }
  42. for(j=0;j<i;j++) //Calculating total number of words and storing the first letter's index in another array
  43. {
  44. if(((a[j]<'a')||(a[j]>'z'))&&((a[j+1]>='a')&&(a[j+1]<='z')))//&&(a[j-1]==' '))
  45. {
  46. k[kp]=j+1;
  47. kp++;
  48. co++;
  49. }
  50. }
  51. for(l=0;l<kp;l++) //sorting the indexes of words in a lexicographical order
  52. {
  53. for(j=1;j<kp;j++)
  54. {
  55. y=0;
  56. if(a[k[j]]<a[k[j-1]])
  57. {temp=k[j-1]; k[j-1]=k[j]; k[j]=temp;}
  58. else if(a[k[j]]==a[k[j-1]])
  59. {
  60. while((a[k[j]+y]==a[k[j-1]+y])&&(a[k[j]+y]!=' ')){y++;}
  61. if(a[k[j]+y]<a[k[j-1]+y])
  62. {temp=k[j-1]; k[j-1]=k[j]; k[j]=temp;}
  63. }
  64.  
  65. }
  66. }
  67. np=1;
  68. for(j=0;j<kp;j++,np++) //Finding and marking duplicates by replacing index by -1
  69. {
  70. o=0,fl=0;
  71. while(isChar(a[k[np]+o]) && isChar(a[k[j]+o]))
  72. {
  73. if(a[k[np]+o]!=a[k[j]+o]){fl=1;}
  74. o++;
  75. }
  76. if(((fl==0))&&((!isChar(a[k[j]+o]))&&(!isChar(a[k[np]+o]))))
  77. {
  78. k[j]=-1;
  79. co--;
  80. }
  81. }
  82. cout<<co<<"\n";
  83.  
  84. for(j=0;j<kp;j++) //Displaying all the words in lexicographical order
  85. {
  86. y=0;
  87. while(((a[k[j]+y]>='a')&&(a[k[j]+y]<='z'))&&(k[j]!=-1))
  88. {
  89. cout<<a[k[j]+y];
  90. y++;
  91. }
  92. if(((a[k[j]+y-1]>='a')&&(a[k[j]+y-1]<='z'))&&(k[j]!=-1))
  93. cout<<"\n";
  94. }
  95. return 0;
  96. }
  97.  
Success #stdin #stdout 0s 2912KB
stdin
2
The completely working solution for IARCS problem word list.
It takes care of repetition of word too.
stdout
15
care
completely
for
iarcs
it
list
of
problem
repetition
solution
takes
the
too
word
working