結果

問題 No.1171 Runs in Subsequences
ユーザー furafura
提出日時 2020-09-06 02:17:53
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 109 ms / 2,000 ms
コード長 2,203 bytes
コンパイル時間 2,112 ms
コンパイル使用メモリ 204,764 KB
実行使用メモリ 56,368 KB
最終ジャッジ日時 2023-08-19 13:22:23
合計ジャッジ時間 4,888 ms
ジャッジサーバーID
(参考情報)
judge15 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,384 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 104 ms
54,456 KB
testcase_16 AC 101 ms
53,668 KB
testcase_17 AC 108 ms
56,360 KB
testcase_18 AC 109 ms
56,292 KB
testcase_19 AC 87 ms
56,368 KB
testcase_20 AC 88 ms
56,312 KB
testcase_21 AC 91 ms
56,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

class mint{
	static const int MOD=1e9+7;
	int x;
public:
	mint():x(0){}
	mint(long long y){ x=y%MOD; if(x<0) x+=MOD; }

	mint& operator+=(const mint& m){ x+=m.x; if(x>=MOD) x-=MOD; return *this; }
	mint& operator-=(const mint& m){ x-=m.x; if(x<   0) x+=MOD; return *this; }
	mint& operator*=(const mint& m){ x=1LL*x*m.x%MOD; return *this; }
	mint& operator/=(const mint& m){ return *this*=inverse(m); }
	mint operator+(const mint& m)const{ return mint(*this)+=m; }
	mint operator-(const mint& m)const{ return mint(*this)-=m; }
	mint operator*(const mint& m)const{ return mint(*this)*=m; }
	mint operator/(const mint& m)const{ return mint(*this)/=m; }
	mint operator-()const{ return mint(-x); }

	friend mint inverse(const mint& m){
		int a=m.x,b=MOD,u=1,v=0;
		while(b>0){ int t=a/b; a-=t*b; swap(a,b); u-=t*v; swap(u,v); }
		return u;
	}

	friend istream& operator>>(istream& is,mint& m){ long long t; is>>t; m=mint(t); return is; }
	friend ostream& operator<<(ostream& os,const mint& m){ return os<<m.x; }
	int to_int()const{ return x; }
};

mint operator+(long long x,const mint& m){ return mint(x)+m; }
mint operator-(long long x,const mint& m){ return mint(x)-m; }
mint operator*(long long x,const mint& m){ return mint(x)*m; }
mint operator/(long long x,const mint& m){ return mint(x)/m; }

int main(){
	string s; cin>>s;
	int n=s.length();

	// dp1[i][c] = ( s[0,i) の部分列であって末尾の文字が c であるものの個数 )
	vector dp1(n+1,vector(26,mint(0)));
	rep(i,n){
		rep(c,26) dp1[i+1][c]=dp1[i][c];

		int c0=s[i]-'a';
		dp1[i+1][c0]+=1;
		rep(c,26) dp1[i+1][c0]+=dp1[i][c];
	}

	// dp2[i][c] = ( s[0,i) の部分列であって末尾の文字が c であるものについての連の個数の総和 )
	vector dp2(n+1,vector(26,mint(0)));
	rep(i,n){
		// s[i] を選ばない
		rep(c,26) dp2[i+1][c]=dp2[i][c];
		// s[i] を選ぶ
		int c0=s[i]-'a';
		dp2[i+1][c0]+=1; // s[i] のみからなる部分列
		rep(c,26){
			if(c==c0) dp2[i+1][c0]+=dp2[i][c];
			else      dp2[i+1][c0]+=dp2[i][c]+dp1[i][c];
		}
	}

	mint ans=0;
	rep(c,26) ans+=dp2[n][c];
	cout<<ans<<'\n';

	return 0;
}
0