333
Cerința
Se dă un șir de caractere care conține cuvinte formate din litere mici ale alfabetului englez și separate printr-un singur spațiu. Să se determine cel mai lung cuvânt care are toate literele distincte. Dacă nu există niciun cuvânt cu toate literele distincte se va afișa -1
.
Date de intrare
Programul citește de la tastatură un șir de caractere.
Date de ieșire
Programul va afișa pe ecran cuvântul determinat sau -1
, conform cerinței.
Restricții și precizări
- șirul dat conține maximum
255
de caractere - dacă există mai multe cuvinte cu toate literele distincte de lungime maximă se va afișa cel mai din stânga
Exemplu
Intrare
abcabcd abcdef ab
Ieșire
abcdef
#include <bits/stdc++.h>
using namespace std;
int f[1000];
char s[1000];
void init()
{
for(int i = 1 ; i <= 1000 ; ++i)
f[i]=0;
}
bool verif(int i)
{
init();
for( ; s[i]!=' ' && s[i]!='\0' ; ++i)
f[(int)s[i]]++;
for(int i = 50 ; i < 200 ; ++i)
if(f[i]>1)
return 0;
return 1;
}
int lung(int i)
{
int cnt = 0;
for( ; s[i]!=' ' && s[i]!='\0' ; ++i)
cnt++;
return cnt;
}
void afis(int i)
{
for( ; s[i] != ' ' && s[i]!='\0' ; ++i)
cout << s[i];
}
int main()
{
int ind = -1;
int max=0;
cin.getline(s , 300);
if(verif(0))
ind = 0 , max = lung(0);
int i=0;
while(s[i]!='\0')
{
if(s[i]==' ' && s[i+1]!=' ' && s[i+1]!='\0')
if(verif(i+1) && lung(i+1)>max)
ind = i+1 , max = lung(i+1);
i++;
}
if(ind!=-1)
afis(ind);
else
cout << -1;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int f[1000];
char s[1000];
void init()
{
for(int i = 1 ; i <= 1000 ; ++i)
f[i]=0;
}
bool verif(int i)
{
init();
for( ; s[i]!=' ' && s[i]!='\0' ; ++i)
f[(int)s[i]]++;
for(int i = 50 ; i < 200 ; ++i)
if(f[i]>1)
return 0;
return 1;
}
int lung(int i)
{
int cnt = 0;
for( ; s[i]!=' ' && s[i]!='\0' ; ++i)
cnt++;
return cnt;
}
void afis(int i)
{
for( ; s[i] != ' ' && s[i]!='\0' ; ++i)
cout << s[i];
}
int main()
{
int ind = -1;
int max=0;
cin.getline(s , 300);
if(verif(0))
ind = 0 , max = lung(0);
int i=0;
while(s[i]!='\0')
{
if(s[i]==' ' && s[i+1]!=' ' && s[i+1]!='\0')
if(verif(i+1) && lung(i+1)>max)
ind = i+1 , max = lung(i+1);
i++;
}
if(ind!=-1)
afis(ind);
else
cout << -1;
return 0;
}
#include <bits/stdc++.h> using namespace std; int f[1000]; char s[1000]; void init() { for(int i = 1 ; i <= 1000 ; ++i) f[i]=0; } bool verif(int i) { init(); for( ; s[i]!=' ' && s[i]!='\0' ; ++i) f[(int)s[i]]++; for(int i = 50 ; i < 200 ; ++i) if(f[i]>1) return 0; return 1; } int lung(int i) { int cnt = 0; for( ; s[i]!=' ' && s[i]!='\0' ; ++i) cnt++; return cnt; } void afis(int i) { for( ; s[i] != ' ' && s[i]!='\0' ; ++i) cout << s[i]; } int main() { int ind = -1; int max=0; cin.getline(s , 300); if(verif(0)) ind = 0 , max = lung(0); int i=0; while(s[i]!='\0') { if(s[i]==' ' && s[i+1]!=' ' && s[i+1]!='\0') if(verif(i+1) && lung(i+1)>max) ind = i+1 , max = lung(i+1); i++; } if(ind!=-1) afis(ind); else cout << -1; return 0; }
Comentarii