- Joined
- Apr 24, 2022
- Messages
- 1
- Reaction score
- 0
I've been practicing interview and cp question for some time now and I'm really stuck at this problem for a really long a time
The answer
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
string s[n];
int m=0;
int ans=0;
for(int i=0;i<n;i++)
{
cin>>s;
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n-1;j++)
{
m=string(s[j]+s[j]).find(s);
ans+=m;
}
}
cout<<ans<<endl;
4
xzzwo
zwoxz
zzwox
xzzwo
My answer is 10 however the optimal solution is 5
How do I get about it and what do i need to do
}
What my code is lacking is the most optimal solution
For example
Problem - 798B - Codeforces
Codeforces. Programming competitions and contests, programming community
codeforces.com
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
string s[n];
int m=0;
int ans=0;
for(int i=0;i<n;i++)
{
cin>>s;
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n-1;j++)
{
m=string(s[j]+s[j]).find(s);
ans+=m;
}
}
cout<<ans<<endl;
4
xzzwo
zwoxz
zzwox
xzzwo
My answer is 10 however the optimal solution is 5
How do I get about it and what do i need to do
}
What my code is lacking is the most optimal solution
For example