Jump to content

Rfd

Members
  • Posts

    4
  • Joined

  • Last visited

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

Rfd's Achievements

Newbie

Newbie (1/14)

10

Reputation

  1. Rfd

    [C/C++]Palindrome

    Va multumesc mult!
  2. Timus Online Judge. Problem 1297 //#include "stdafx.h" #include <iostream> #include <string> using namespace std; string s1, s2, s3, smax; int i, j, n, m, pos, lol, maxt; int main () { cin>>s1; maxt=0; for (i=1;i<=s1.length();i++) for (j=0;j<s1.length();j++) { s2=s1.substr(j, i); s3=s2; reverse(s3.begin(),s3.end()); if (s2==s3) { pos=s1.find(s2); if (pos!=string::npos) { lol=s2.length(); if (lol>maxt) { maxt=lol; smax=s2; } } } } cout<<smax; system("pause"); return 0; } Pe acest cod C++ am TLE (Time limit exceeded) pe testul 15. Am facut acest topic pentru a cere sfaturi in legatura cu solutia problemei. Va multumesc!
  3. Problema era legata de siruri de caractere cu aceasta proprietate, nu numere palindrom.
  4. #include "stdafx.h" #include <iostream> #include <string> #include <cctype> using namespace std; int main () { string s1,s2; string::iterator it; getline(cin,s1); for (it=s1.begin();it<s1.end();it++) { if (*it>='A' && *it<='Z') *it+=32; if (isalpha(*it)) s2+=*it; } s1.clear(); string::reverse_iterator rit; for (rit=s2.rbegin();rit<s2.rend();rit++) s1+=*rit; if (s1.compare(s2)==0) cout<<"Palindrom"; else cout<<"Nu este palindrom"; system("pause"); return 0; } Cod C++. Acest cod poate fi simplificat prin utilizarea functiilor implicite (de exemplu: reverse() din <algorithm>). ______________________________________________ #include "stdafx.h" #include <iostream> #include <string> #include <cctype> using namespace std; int main () { string s1,s2; string::iterator it; getline(cin,s1); for (it=s1.begin();it<s1.end();it++) { if (*it>='A' && *it<='Z') *it+=32; if (isalpha(*it)) s2+=*it; } s1.clear(); string::reverse_iterator rit; for (it=s2.begin(),rit=s2.rbegin();it<s2.end(),rit<s2.rend();it++,rit++) if (*it!=*rit) { cout<<"Nu este palindrom"; system("pause"); return 0; } cout<<"Palindrom"; system("pause"); return 0; } C++ Problema rezolvata fara inversarea string-ului. ______________________________________________ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> char x[1001],y[1001]; int main () { int i,j,n; gets(x); n=strlen(x); j=0; for (i=0;i<n;i++) { if (isalpha(x[i]) && x[i]<='Z' && x[i]>='A') x[i]+=32; if (isalpha(x[i])) { y[j]=x[i]; j++; } x[i]='\0'; } n=strlen(y); for (i=0,j=n-1;i<n,j>=0;i++,j--) if (y[i]!=y[j]) { printf("Nu este palindrom"); system("pause"); return 0; } printf("Palindrom"); system("pause"); return 0; } Rezolvare C.
×
×
  • Create New...