Reputation: 839
I have been scratching my head to figure out why is my program is crashing. My aim was to scan a string and get frequency of each sub string !
The Real part where the program is crashing (M is a map of string,int type) My Input: the string is "abab" and the program crashes when i=0 and j is equal to 3 just at the M[e]++ statement!
for(i=0;str[i];i++)
{
char temp[5001];
k=0;
cout<<str[i]<<endl;
for(j=i;str[j];j++)
{
temp[k]=(char)str[j];
k++;
temp[k]='\0';
string e(temp);
M[e]++;
cout<<j<<endl;
}
}
MAIN Method
int main()
{
ini();
int t,N,i,j,Q,buff,k=0;
char str[5001];
scanf("%d",&t);
map <string ,int > M;
map <string , int >::iterator ii;
for(;t--;)
{
scanf("%d%d",&N,&Q);
scanf(" %s",str);
for(i=0;str[i];i++)
{
char temp[5001];
k=0;
cout<<str[i]<<endl;
for(j=i;str[j];j++)
{
temp[k]=(char)str[j];
k++;
temp[k]='\0';
string e(temp);
M[e]++;
cout<<j<<endl;
}
}
for(ii=M.begin();ii!=M.end();++ii)
F[ii->second]++;
F2[N]=F[N]%MOD;
for(i=N;i>=1;i--)
if(F[i])
for(j=i-1;j>=1;j--)
F2[j]=(F[j]+((long long)F[i]%MOD*C(F[i],j)%MOD)%MOD)%MOD;
for(i=0;i<Q;i++)
{
scanf("%d",&buff);
printf("%d\n",F2[buff]);
}
}
return 0;
}
Note
int F[5001],F2[5001];
are declared globally too.
Upvotes: 0
Views: 86
Reputation: 5388
for(i=0;str[i];i++)
{
for(j=0;str[j+i];j++)
{
M[str.substr(j,i+1)]++;
}
}
Replace internal two for loops with this and check whether it's crashing or not . If still that means you may be running this program on windows.In that case use this.
for(i=0;str[i];i++)
{
for(j=0;str[j+i];j++)
{
std::string sstr = str.substr(j,i+1);
if ( M.find ( sstr ) == M.end() ){
M.insert( std::make_pair ( sstr , 0 ) ) ;
}
else
M[str.substr(j,i+1)]++;
}
}
Upvotes: 0
Reputation: 853
As requested:
#include <iostream>
#include <string>
#include <map>
#define MOD 10
using namespace std;
int C( int a, int b ){
return 5;
}
int F[5001],F2[5001];
int main()
{
int t,N,i,j,Q,buff,k=0;
string str(5001, ' ');
cin >> t;//scanf("%d",&t);
cin.ignore( 256, '\n' );
map <string ,int > M;
map <string , int >::iterator ii;
for(;t--;)
{
cin >> N;
cin.ignore( 256, '\n' );
cin >> Q;
cin.ignore( 256, '\n' );
//scanf(" %s",str);
getline(cin,str);
for(i=0;str[i];i++)
{
char temp[5001];
k=0;
cout<<str[i]<<endl;
for(j=i;str[j];j++)
{
temp[k]=(char)str[j];
k++;
temp[k]='\0';
string e(temp);
M[e]++;
cout<<j<<endl;
}
}
for(ii=M.begin();ii!=M.end();++ii)
F[ii->second]++;
F2[N]=F[N]%MOD;
for(i=N;i>=1;i--)
if(F[i])
for(j=i-1;j>=1;j--)
cout << "hello";F2[j]=(F[j]+((long long)F[i]%MOD*C(F[i],j)%MOD)%MOD)%MOD;
for(i=0;i<Q;i++)
{
scanf("%d",&buff);
printf("%d\n",F2[buff]);
}
}
return 0;
}
For testing purposes, because there was no MOD
and C
definitions given, for MOD
I used a constant int
and C
an empty function that received those parameters and simply returned a value.
Instead of scanf
, I used cin
for the inputs and later cin.ignore()
to clear the input buffer so that it won't skip the next cin
. Changed str
to type string
. Used getline
to get input for string, as this reads the enitre line from the input cin
. And that is it for modifications.
Upvotes: 1