Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Set sub sequences.
//clang 3.8.0 #include <iostream> #include <vector> const int MAX_CHAR = 256; int countSub(std::string str); int main() { // std::string str="Hello"; //const char *item = str.c_str(); std::string str; std::cin>>str; int testCase; std::cin>>testCase; while(testCase--){ int start=0; int end=0; std::cin>>start>>end; std::string concat2; for(int i=start-1;i<end-1;i++){ concat2+=str[i]; // std::cout<<concat2; } int output = countSub(concat2); std::cout<<output<<std::endl; } } int countSub(std::string str){ std::vector<int> last(MAX_CHAR,-1); int n = str.length(); int dp[n+1]; dp[0] = 1; // Traverse through all lengths from 1 to n. for (int i=1; i<=n; i++) { // Number of subsequences with substring // str[0..i-1] dp[i] = 2*dp[i-1]; // If current character has appeared // before, then remove all subsequences // ending with previous occurrence. if (last[str[i-1]] != -1) dp[i] = dp[i] - dp[last[str[i-1]]]; // Mark occurrence of current character last[str[i-1]] = (i-1); } return dp[n]-1; }
run
|
edit
|
history
|
help
0
Exploring stringstreams
Merge Sort
marquee text in C++
Assignment Operator Example
HTML Timetable generator.cpp
DESim Example with Hash Table
return reference (clang)
Test bitfields with unnamed union
Magic, why 1 2?
Access to temporary object