Friday, April 6, 2018

3939 - Counting substrings COJ Problem Solution

http://coj.uci.cu/24h/problem.xhtml?pid=3939


#include<bits/stdc++.h>

// Nayeem Mollick Joy , University of Rajshahi.

using namespace std;

int main()
{
    string s;
    cin>>s;
    set<string>s1;
    for(int i=0;i<s.size();i++)
    {
        for(int len=1;len<=s.size()-1;len++)
        {
            s1.insert(s.substr(i,len));
        }
    }
    cout<<s1.size()+1<<endl;
    return 0;
}

1462 - Easy Sum COJ Problem Solution

http://coj.uci.cu/24h/problem.xhtml?pid=1462




#include<bits/stdc++.h>
using namespace std;


string findSum(string str1, string str2)
{
    if (str1.length() > str2.length())
        swap(str1, str2);

    string str = "";

    int n1 = str1.length(), n2 = str2.length();
    int diff = n2 - n1;

  
    int carry = 0;

  
    for (int i=n1-1; i>=0; i--)
    {
      
        int sum = ((str1[i]-'0') +
                   (str2[i+diff]-'0') +
                   carry);
        str.push_back(sum%10 + '0');
        carry = sum/10;
    }

   
    for (int i=n2-n1-1; i>=0; i--)
    {
        int sum = ((str2[i]-'0')+carry);
        str.push_back(sum%10 + '0');
        carry = sum/10;
    }

  
    if (carry)
        str.push_back(carry+'0');


    reverse(str.begin(), str.end());

    return str;
}

// Driver code
int main()
{
    int tc;
    cin>>tc;
        string str1 ;
    vector<string>v;
    while(tc--)
    {
        cin>>str1;
        v.push_back(str1);
    }
    string str2 = "0";
    for(int i=0;i<v.size();i++)
    {
        str2=findSum(str2,v[i]);
    }
     int n=0;

            for(int h=0;h<str2.size();h++)

            {

            n=n*10+(str2[h]-'0');
                n=n%128;

        }
    cout<<n<<endl;
    return 0;
}