求序列逆序数
很简单,是由一个题启发的
#include <iostream> #include <numeric> using namespace std; int main() { int a[100]; string s; while(cin>>s) { a[0] = 0; for(int i = 1; i < s.size(); i++) { a[i] = a[i-1] + (s[i]<s[i-1]); } int countt = 0; for(int i = 1;i < s.size(); i++) { countt += a[i]; } cout << countt << endl; } }
另附改题目:
大概意思是先给出一个串s,然后给出m行,每行给出l,r两个数求满足s[i+1]==s[i];的个数.l<=i<r;
#include <iostream> using namespace std; int num[100010]; int main() { string s; while(cin>>s) { for(int i = 1; i < s.size(); i++) { num[i] = num[i-1] + (s[i] == s[i-1]); } int n; cin>>n; while(n--) { int a, b; cin>>a>>b; cout << num[b-1] - num[a-1] << endl; } } }